Jump to content

  • Log In with Google      Sign In   
  • Create Account


wierd linked list deletion problem


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 geoteamy   Members   -  Reputation: 101

Like
0Likes
Like

Posted 13 November 2012 - 12:52 PM

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;


Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 7407

Like
2Likes
Like

Posted 13 November 2012 - 12:58 PM

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.

Edited by Brother Bob, 13 November 2012 - 12:58 PM.


#3 Brother Bob   Moderators   -  Reputation: 7407

Like
2Likes
Like

Posted 13 November 2012 - 01:04 PM

Also, your if-statement should check if root is null, not if root->next is null.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS