wierd linked list deletion problem

Started by
1 comment, last by Brother Bob 11 years, 5 months ago
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



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



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.



int main()
{
int test = 0;
zombie master;
zombie *root = &master;
zombie *temp=NULL;
Advertisement
If root in the second code is the same as the root in the first code, then you should not call delete on it because you didn't new it. You must not delete what you didn't new.
Also, your if-statement should check if root is null, not if root->next is null.

This topic is closed to new replies.

Advertisement