So my code passes all the Check50 tests apart from the memory one. I have been playing around with this for ages, but don't understand what the problem is. Here's what valgrind says. Valgrind error note

And here's the rest of the Valgrind (and Help50) output: Valgrind Heap Summary

And here is the section of my code that help50 says is the problem (line 31 is the for loop: for(node *tmp...): code at issue

Anybody got any ideas what the issue might be?


So it turns out I was misreading Valgrind. The part of the code I listed above doesn't show the source of the problem. Once I had figured that out, I asked another question here:

PSET5/Speller - Valgrind error: conditional jump, relates to table[i] array, but I've already initialized that (or have I?)?

In the Valgrind print out above, I should have been looking at lines 57 and 71. I had forgotten to initialize a malloced pointer in my node structure: The missing line was n->next = NULL after the malloc.

enter image description here


My first guess is because of the 'tmp->next' iterator towards the end, once you have reached towards the end of the linked list, i.e. next pointer to NULL, the for loop still does not end. E.g. when tmp = NULL, temp->next will still iterate.

Does it make sense if you end the for loop when temp->next is NULL? Maybe adding this if condition within the for loop as well

if(tmp->next == NULL) { break }

  • Thanks, but I don't think that's it. The loop itself works perfectly so I'm not iterating too far. If I were iterating too far I'd get a Segmentation Fault, right? The program works fine apart from this Valgrind error. – cormacl Dec 16 '20 at 19:49
  • Hey there, so I have subsequently realized that the problem seems to be related to the array table[i]. But I don't quite understand it yet, because I did initialize table[i] to NULL with a loop. – cormacl Dec 19 '20 at 15:16

You must log in to answer this question.

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