Jump to content
  • Advertisement
Sign in to follow this  
Twinsen2

Link List- - - Deleting an element yeilds Error :(

This topic is 4830 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hey, i am making a link list for a sort of ranking system, much like that in counter strike, the problem i am having is that when i go 2 delete an element the program crashes and i cant figure out wat is wrong with it. I have the adding elements to the list working fine, just the deleting bit.
Iterator List::erase(Iterator pos)
{
	Iterator iter;

	assert(iter.location != NULL);

	Node* remove = iter.location;
	Node* before = remove->backward;
	Node* after = remove->forward;

	if (remove == first)
	{
		first = after;
	}else
	{
		before->forward = after;
	}

	if (remove == last)
	{
		last = before;
	}else
	{
		after->backward = before;

		delete remove;
		
	}
	Iterator i;
	return i;
}
thats the delete function for the iterator. here is the main where the list is created "Note this is just test data"
	List gavan;
	gavan.send_back("test1");
	gavan.send_back("test1");
	gavan.send_back("test1");
	gavan.send_back("test1");
	gavan.send_back("test1");
	gavan.send_back("test1");	
	gavan.send_back("test1");

	gavan.insert(gavan.begin(),"hello");
	gavan.erase(gavan.begin());
here is the begin function:
Iterator List::begin()
{
   Iterator iter;
   iter.position = first;
   iter.last = last;
   return iter;
}
hmzzz yeah when i try and delete an element the windows error reporting tool pops up, just wondering if any 1 can help. Thankz very much in advance [Edited by - Twinsen2 on July 30, 2005 10:42:34 PM]

Share this post


Link to post
Share on other sites
Advertisement
hmmmz tried that and i got this:

Unhandled exception at 0x0041aa75 in ll.exe: 0xC0000005: Access violation reading location 0xcccccccc.

halted on code line:

Node* before = remove->backward;

this is from the erase function from above.

still stuck though cause it should work :/

Share this post


Link to post
Share on other sites
Most likely you are trashing memory.

I have had the same thing happen some time ago, I believe that I got around it by copying the node to a temporary local memory, deleting it, then I can still access back pointers and forward pointers form there.

But I could be mistaken.

Share this post


Link to post
Share on other sites
had a look and first is being initalised properely, because its used in the insert function, and that works.....hmzzzz.....

if you want i could post the whole code?? maybe that would make it clearer?

Share this post


Link to post
Share on other sites
Quote:

if you want i could post the whole code?? maybe that would make it clearer?


please.

[edit: no, wait. you're using the local iter:

Iterator iter;
assert(iter.location != NULL);
Node* remove = iter.location;

without ever assigning iter to the container. And the parameter passed in [pos] is never used.]

Share this post


Link to post
Share on other sites
MAN>.....THANK YOU SO MUCH......
I Got it.....thankz a million..
You have no idea hwo releived i am.
Thankz ...lol

Yeah i no wat u mean and i chnaged it to:

Iterator List::erase(Iterator pos)
{
Iterator iter(pos);

assert(iter.location != NULL);

Node* remove = iter.location;
Node* before = remove->backward;
Node* after = remove->forward;


I plassed the pos through the container.

Thankz a maillion!!!!



Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!