Using the DevC++ debugger (still new to it), I’m pretty sure I’ve isolated a segfault scenario, but I’ve got no idea why it’s happening.
void LinkedList::Add(const char *Word){
ListNode *newNode = new ListNode;
strcpy(newNode->Word, Word);
newNode->Next = NULL;
...
}
A segfault occurs at the “newNode->Next = NULL;”. However if I remove the strcpy above it, the segfault does not occur (but it means my newNode->Word is empty)
EDIT: sorry guys, here is the ListNode:
struct ListNode
{
char *Word;
LNodePtr Next;
};
You have not allocated any memory for
Wordpointer in theListNodestructure. Without this, it is pointing to some random location and whenever you are trying to write to that location (usingstrcpy) you will get an access violation. The simplest way to solve this is to use thestd::stringclass and get rid of all manual memory management.If you really want to use
char*, then write a constructor forListNodewhich takesconst char*parameter (remember to declare it asexplicitthough) and use thestrlento find the length of the input string. Then allocatelen + 1(extra onecharfor the NULL terminator) characters and store the address inWordpointer. After that you can dostrncpy.