I am trying to add every node in a linked list together to get a new linked list. My program compiled, but when I tried to execute the add method I got a Segmentation fault. I know that this means I screwed up my pointers somehow, but I have no clue how. Here's my add function.
Right (it is true for temp2 and temp3, but temp = head is not defined in this scope). Anyway, the while loop only check if temp2->next is not null. Can it be the case that temp->next or temp3->next run into null before temp2->next does (lines 34-35)?
That most certainly could be the case, but I assumed it wouldn't matter and would simply add nothing. Temp3 is for my New object, which is where I am trying to store the sum of my Native Object and Num. That's what I am trying to do anyway.
It could be crashing because temp becomes NULL on line 33, then it's dereferenced on line 22. This would occur if *this (the object calling Add ) has fewer nodes than Num does, as JockX has pointed out.
If *this has more nodes then instead of crashing, the extra nodes get ignored and a wrong result is produced. This is also bad.
temp3 is probably iterating OK, but it serves no purpose in the program as it isn't being used to do anything (it shouldn't be needed to make InsertTail() work right, if that's what you're thinking).
Also, lines 14-16 are pointless. See my previous post.
Does it crash when *this has more (or equal) nodes than Num?