//The deconstructor
~CLinkedList()
{
//Declares the current link
Vertex *Current = First;
//A temporary pointer
Vertex *TempVertex = new Vertex;
while(Current != NULL)
{
//Sets TempVertex to Current
*TempVertex = *Current;
//Sets the current link to the next link
Current = Current->Next;
//Deletes the TempVertex
delete TempVertex;
}
//Deletes Current
delete Current;
//Deletes First
delete First;
}
*Edited In*
Do I even need memory managment if the object is with the program while its running. I mean if I never stop using the object, is it deleted when the program stops running?
Edited by - Taphreek on January 30, 2001 6:49:25 PM
Memory Mangement
I have been working on some simple pointers and was wondering if anyone can tell me if this is doing what I want it to. I want the deconstructor to go through the linked list(singly) I have and delete all the stuff that's inside it. Here's the code. Also I am correct in assuming that if a constructor automatically gets called on "contruction" a deconstructor will automatically be called when the object is destroyed?
You should have memory management and free up your allocations just in case you need to create and remove instances in the future. Always clean up your own mess and don''t leave it for someone else to do, then you won''t get bitten when you least expect it.
As for the code, my C pointer knowledge is a little rusty at the moment, but I don''t think that will work.
The last two delete''s shouldn''t be there. TempVertex shouldn''t be initialised, and should be set with
TempVertex = Current;
I think that should do it, but VC (if you have it) will warn you of memory leaks in debug mode when you exit.
As for the code, my C pointer knowledge is a little rusty at the moment, but I don''t think that will work.
The last two delete''s shouldn''t be there. TempVertex shouldn''t be initialised, and should be set with
TempVertex = Current;
I think that should do it, but VC (if you have it) will warn you of memory leaks in debug mode when you exit.
lose the ''new'' and lose the dereferencing bit.
it sould look like this:
i think that should do it.
it sould look like this:
//The deconstructor~CLinkedList(){//Declares the current link Vertex *Current = First; //A temporary pointer Vertex *TempVertex = NULL; while(Current != NULL) { //Sets TempVertex to Current TempVertex = Current; //Sets the current link to the next link Current = Current->Next; //Deletes the TempVertex delete TempVertex; }}
i think that should do it.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement