Hi everyone, if I delete nodes from a linked list, why does ptr->pPrev not already point to NULL

Hi everyone, in a previous post I was having a strange issue where I couldn't trace the problem. I found the reason it broke, but I'm not sure why this is a problem. This worked in Visual Studio, but broke in unix. Here is the code



THIS WORKED IN VISUAL STUDIO BUT NOT IN LINUX
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  template <class T>
void List<T>::clear()
{
	Node<T> * WillDelete = frontPtr;
	while (WillDelete != NULL)
	{
		WillDelete = frontPtr->pNext;
		delete frontPtr;
		frontPtr = WillDelete;
	}
	backPtr = NULL;
	numberOfNodes = 0;
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
template <class T>
void List<T>::clear()
{
	Node<T> * WillDelete = frontPtr;
	while (WillDelete != NULL)
	{
		WillDelete = frontPtr->pNext;
		
                frontPtr->pPrev = NULL; // THIS LINE FIXED IT!!!

                delete frontPtr;
		frontPtr = WillDelete;
	}
	backPtr = NULL;
	numberOfNodes = 0;
}


By the way, the problem was that after I cleared my Linked List, my new nodes already had pPrev pointers when they shouldn't have. So my NEW nodes, already had their pPrev pointer value assigned (When it should have been NULL)

I have two questions. First, why the hell didn't this work in linux but did in Visual Studio? By deleting an object, I thought I deleted all of the members and their data. Not to mention, why would a NEW node have pPrev set?

Second of all, this is an easy fix, but am I risking a memory leak? I suddenly feel like I don't understand pointers. Do I have to completely delete something else?
Second of all, this is an easy fix, but am I risking a memory leak?

It's not a fix at all. When you create a node, the node's members must be initialized or assigned to before they are used. Relying on what is already there without doing so is relying on undefined behavior.
You're right Cire. I missed something in my constructor. I underestimated how much of a pain that would be.
Topic archived. No new replies allowed.