Skip to main content
GameDev.net gamedev.net
🔒 Locked

wierd linked list deletion problem

Started by geoteamy Nov 13, 2012 at 6:52 PM 1 replies 1k views
Original Post
geoteamy
geoteamy
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;
Brother Bob
Brother Bob
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.
Brother Bob
Brother Bob
Also, your if-statement should check if root is null, not if root->next is null.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.