Sign in to follow this  
geoteamy

wierd linked list deletion problem

Recommended Posts

geoteamy    101
I am having a weird problem were i have a linked list system that creates objects and it works fine until i add any numeric variable to the main loop then it crash when it is deleting the objects


[CODE]
while (1)//this deletes the objects
{
if (root->next != NULL)
{
temp=root->next;
delete root;
root = temp;
}
else
{
break;
}
}
[/CODE]


This next part is the beginning of the main loop it works fine until i added the variable "test" after i added it, it crashes whenever i try to delete the whole list using the code above.


[CODE]
int main()
{
int test = 0;
zombie master;
zombie *root = &master;
zombie *temp=NULL;
[/CODE]

Share this post


Link to post
Share on other sites
Brother Bob    10344
If [i]root [/i]in the second code is the same as the [i]root [/i]in the first code, then you should not call [i]delete [/i]on it because you didn't [i]new[/i] it. You [b]must not[/b] [i]delete[/i] what you didn't [i]new[/i]. Edited by Brother Bob

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this