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
.
cursor
isNULL
, then trying to readcursor->next
will result in segmentation fault.cursor
? What ifcursor
itself is equal toNULL
?