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

Started by
7 comments, last by Twinsen2 18 years, 8 months ago
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]
Advertisement
I recommend that you run it in the debugger. That should tell you exactly where and why it it crashing.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
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 :/
looks like you're not initializing 'first' properly. [/guess]
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.
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?
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.]
ohhh cheerz man...
But doesnt the code:

Iterator iteri
[\code]

doesnt this assign it to the container??
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!!!!



This topic is closed to new replies.

Advertisement