So I'm building a a linked list class, with stack-like functionality (LIFO), to hold instances of a Node class:
enum Sides{NorthWest=0, North=1, NorthEast=2, West=3, East=4, SouthWest=5, South=6, SouthEast=7};
class Node
{
public:
Node(position2df pos, int id):nextNode(NULL)
{
position=pos;
ID=id;
}
~Node(){}
position2df getPosition(){return position;}
int getID(){return ID;}
void setPeripheralID(Sides side, int id){peripheralID[side]=id;}
int getPeripheralID(Sides side){return peripheralID[side];}
Node *nextNode;
private:
position2df position;
int ID;
int peripheralID[8];
};
class NodeList
{
public:
NodeList()
{
root=NULL;
end=NULL;
}
~NodeList(){}
/// Function for adding elements to the list.
void push(position2df pos, int id)
{
if(root==NULL)
{
root=new Node(pos, id);
end=root;
}
else
{
Node *newend=new Node(pos, id);
end->nextNode=newend;
end=end->nextNode;
}
}
/// Function for removing objects from the list.
Node *pop()
{
slider=root;
Node *previous;
Node *next=slider;
for(previous=NULL; next!=NULL; next=slider->nextNode)
{
previous=slider;
slider=next;
}
delete slider;
end=previous;
cout << "Can still access deleted object: " << (*slider).getID() << endl;
return end;
}
private:
Node *root;
Node *end;
Node *slider;
};
In the NodeList::Node *pop()
function (whose purpose is to delete the last element and reset the previous one as the end of the list), I call delete on the Node class
instance pointed by (pointer name) slider. However, even after deleting it I can still access the instance and output its members. I don't know if it matters, but the instance has three different pointers pointing to it at the time of its deletion. They are:
- Node *slider;
- Node *end;
- the previous class instance's Node::Node *nextNode;
I suppose at this point a question is in order :D
If I can still access the instance members after I deleted it how do I know if it was properly deleted?
Is my code going to cause a memory leak?
I guess the ultimate question is: am I doing something wrong?
P.S. There are a couple of variables here (position2df; vector2df;
) that come from the Irrlicht Game Engine. Just wanted to point that out to avoid any confusion.
Forgive me if I was vague or otherwise unclear with this post, but I'm not very skilled when it comes to asking questions online.