In speller I get a segmentation fault, what is strange that when i run the programm through valgrind it doesn't crash... In GDB I backtraced that there is a problem when trying to free(woord2). Anybody see the problem? In the light of academic honesty i just posted the part I think is important, if something else is needed, please let me know. https://gist.github.com/anonymous/177eed6b6faf9750602e

  • This one's got me scratching my head. The only thing I can think of is if you were trying to free a nonexistent space. I'm not even sure that would cause a seg fault. What is woord2 pointing at when the seg fault occurs? Is it NULL? Is it possible that it was being malloc'd with a size of 0? This might happen when word is empty. There are two free() commands. Which is causing the fault? Or does it vary, depending on the input? Just to be sure, is it one of the free(woord2) statements causing the seg fault, or another statement very near to them? – Cliff B May 3 '15 at 19:36
  • It actually is the first free... Woord2 is not empty, unfortunately. Woord2 actually matches one of the words in the preloaded dictionary. I am Totally confused About this.. – Mihaly May 3 '15 at 19:47
  • I discussed this with @ebobtron over on cs50x.slack.com He came up with what we think is your problem. char *woord2 = malloc (strlen(word)); may be the root cause because it isn't allocating the space at the end for the null character that terminates the string. We think that when you do the strcpy, you're going past the end and you are then trying to free memory past the end. Try this: char *woord2 = malloc (strlen(word) + sizeof(char)); It may fix it or it may not, but that's our best thought. I haven't been able to duplicate it, so let us know if it works or not. – Cliff B May 3 '15 at 20:43
  • Thanks a lot! I'll check tomorrow. Even if it is not the problem, still this is indeed a bug! – Mihaly May 3 '15 at 20:46
  • I think Cliff stated our thinking clearly, thought I would add this for confirmation. The C library function size_t strlen(const char *str) computes the length of the string str up to, but not including the terminating null character. – ebobtron May 4 '15 at 2:33

Answered in comments by Cliff, OP's problem solved.

Strings, in C when using strlen and strcpy, are considered as NULL terminated character arrays which we happen to call strings. If your going to use the string functions be sure your character buffers are large enough to account for all characters and the terminating NULL.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .