I am writing a helper function that is to be used by both a copy constructor and an overloaded assignment operator. The function is to copy a linked list that has several pointers: headPtr, precursor, cursor, and tailPtr. I think I got the function working okay, but the problem is when I get to the end of the list there is an additional garbage value which is printed out. When I look through the debugger, it looks like cursor is pointing to a next node which is the garbage value printed. I have tried setting cursor->next to nullptr when reaching the end of the list, as well as a lot of wrestling with the code but its not working. How do I get the end of the list to stop pointing correctly?
Below is what the debugger gives for cursor, and -842150451 is the extra garbage value that is printed. The rest of the list before that is okay.
The reason is line 18. The member next of copyList is set to new but probably uninitialized Node while cursor/precursor is set to copyList. I don't know what it is good for but if you want nullptr for next in cursor/precursor you need to change line 18 to:
I looked at the link and can see that the compiler gives 0xCD for addresses generated with new. Seems to be for the purpose of creating patterns that make debugging easier.
What I am understanding is that -842150451 is just some garbage value because it has not been initialized. It is not supposed to be initialized though. For the list 1 - 60, when it hits 60 it's supposed to stop, but prints the additional garbage value -842150451.
Sequence::Sequence() appears to be initializing everything to nullptr or zero, except next, which is left uninitialized. If you fix that I'm confident the bug will go away.
I added next(nullptr) to the list in the default constructor, but I get a red squiggly line under "next" that says "next is a nonstatic member or base class..."
The Node data and next members are organized in a struct in the private section of the Sequence class. I tried using the scope resolution operator, Node::next(nullptr) , to get the compiler to recognize next but that didn't work. Also tried Sequence::Node::next(nullptr) with no luck.
I added next(nullptr) to the list in the default constructor, but I get a red squiggly line under "next" that says "next is a nonstatic member or base class..."
Post how your constructor looks with next added to the initialization list.