Error when using Nodes in C++

Error when using Nodes in C++
Last edited on
you didn't check that curr-> next is not null. Depending on how you code it, these types of data structures can require you to always be checking current against null, current.next against null, and sometimes even current.next.next vs null. You always want to handle the code correctly if

- the list is empty
- the list only has 1 or 2 entries
- the item looked for isn't in there
- the insert position is first or last place

those sorts of situations where the processing is handling something on either end of the list need special attention.


@jonnin is mostly correct. Actually, it's not curr->next that needed to be checked (in this specific case), but it was curr, as stated in your error message. curr is assigned from front, so in this case your list was empty, thus front was NULL, so when you assigned curr and then dereferenced it, you dereferenced a NULL pointer and the program crashed.

Everything that @jonnin said about checking lists and nexts and next->nexts is correct. I recommend drawing pictures of different scenarios (like @jonnin listed -- and don't forget deletions, too) to make sure your code handles all of the situations you need it to handle without leaking memory or dereferencing uninitialized pointers.

Edit: uninitialized, not initialized.
Last edited on
sorry, curr is indeed what needs the test.

Putting on my professor hat for a sec... the 2 things to learn from these things are that pointers are messy and prone to bugs and best avoided AND how the various data structures work (sort of, internally a production data structure is not going to be doing the new-per-node approach and likely has a vector doing all the real work).



Putting on my professor hat for a sec... the 2 things to learn from these things are that pointers are messy and prone to bugs and best avoided

I don't think that the pointers are the problem. I think the problem is that people don't check their vars before they use it. The same problem occurs for example with the stl containers when you access some element from an empty vector or string. Another problem is that people don't learn any testing or debugging in their courses.
That is all true. But the schools teach that you would do a linked list by grabbing memory here, memory there, memory everywhere as you allocate node by node. They don't teach allocating a vector behind the scenes and handing out pointers that refer to vector locations instead, or that you can sort the vector and re-pointer your class to sort the thing in a reasonable time, etc. Of course you would just use list anyway, but what if you had arrays or pointers and no stl? Its like they intentionally teach the worst approaches. And you see this all over, from teaching bubble sort (which is slower, harder to code, and generally terrible) instead of shell sort (shorter, easier, and actually runs at production/industry acceptable speeds). Or they teach the goofy factorial computation and never show that its best as a lookup table. I know one isn't going to be coding up a sort once they get a job, but why teach the worst possible one? All you learn from a lot of these exercises is how not to do stuff.


I guess the teachers use the stuff from their old C courses where these things were necessary and just renamed it to C++ course..
Please DON'T delete your post once you've received your answer. It makes the thread useless as a reference for other people learning C++. It's a selfish abuse of this forum.
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/general/217866/
Topic archived. No new replies allowed.