2

I have finished implementing pset6; however, I get a segmentation fault, when trying to run it. In valgrind I get this error message:

==2741== Invalid read of size 4
==2741==    at 0x8049277: load (dictionary.c:77)
==2741==    by 0x8048875: main (speller.c:45)
==2741==  Address 0x30 is not stack'd, malloc'd or (recently) free'd.

In this case speller line 45 is just when load() is called. Dictionary line 77 is the following line: while ((cursor->next) != NULL). After that I traverse the linked list. I don't know where the error is from, as I have called malloc everytime a initialised new_node. Does anyone know where the error could be from?

  • If cursor is NULL, then trying to read cursor->next will result in segmentation fault. – Kareem Sep 14 '14 at 17:55
  • But won't that never happen, as I am checking whether cursor->next is NULL or not – Kevin Wu Sep 14 '14 at 18:10
  • where exactly are you doing that? – Kareem Sep 14 '14 at 18:26
  • In line 77 as I mentioned above. In fact the loop inside it with the linked list won't execute if cursor->next is NULL – Kevin Wu Sep 14 '14 at 18:45
  • Again, what about cursor? What if cursor itself is equal to NULL? – Kareem Sep 14 '14 at 18:49
1

The problem is that you used malloc to allocate space for your nodes without proper initialisation.

Your error message was:

==2741== Invalid read of size 4
==2741==    at 0x8049277: load (dictionary.c:77)
==2741==    by 0x8048875: main (speller.c:45)
==2741==  Address 0x30 is not stack'd, malloc'd or (recently) free'd.

The error message says that you are trying to read something from address 0x30, which apparently has not been allocated for your program.

You say that the error occurs on the line while ((cursor->next) != NULL). This means that (for your node cursor) next points to address address 0x30.

As address 0x30 has not been allocated to your program, we can safely assume that you did not put 0x30 in next yourself. this means that you did not properly initialise next when you created the node 'cursor' (that is: the node that cursor currently points to).


The easiest way to solve this, might be to use calloc in stead of malloc.

But strictly spoken, that would mean the you rely on the fact that calloc clears the memory in such a way that next ends up having the value NULL. In most languages, that would mean that the program might possibly break in the very next version of the language ...


A "better" way to solve this, might be to set next to NULL whenever you create a new node.

Please note that you would have to do that for every pointer in your structure. So if calloc works, that might be the better option ...

In a situation like this, it might be wise to google for the best practices, for this situation, for the language our working in. I cannot do that for you, as I don't know yet which language is used in pset6.

| improve this answer | |

You must log in to answer this question.

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