What is wrong with this Linked List destructor?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

What is wrong with this Linked List destructor?

Post by xiphirx »

Code: Select all


LinkedList::~LinkedList()
{
	while (first != 0) 
	{
		NodePointer ptr = first;
		first = ptr->next;
		delete ptr;
    }
	if (first == 0) cout << "List destroyed\n";
	else cout << "List not destroyed\n";
}


I always get a "Debug Assertion Failed!"
Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

the code that is being executed

Code: Select all

   {
      LinkedList anotherList;
      for (int i = 0; i < 5; i++)
         anotherList.insert(i, 20 * i);
      cout << "\nHere's another list:\n" << anotherList << endl;
      cout << "Now destroying this list\n";
   }
   cout << "*** If the destructor was called, anotherList was destroyed ***\n";
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
User avatar
Milch
Chaos Rift Junior
Chaos Rift Junior
Posts: 241
Joined: Sat Jul 11, 2009 5:55 am
Programming Language of Choice: C++
Location: Austria, Vienna

Re: What is wrong with this Linked List destructor?

Post by Milch »

Do you set everytime you create a new object the pointer to the next element to NULL?
And by the way, start writing NULL instead of "0" for pointer comparisons ;)
Follow me on twitter!
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: What is wrong with this Linked List destructor?

Post by xiphirx »

Milch wrote:Do you set everytime you create a new object the pointer to the next element to NULL?
And by the way, start writing NULL instead of "0" for pointer comparisons ;)
Yes, whenever a node is created, its "next" is set to "NULL".
I usually use NULL, but I was experimenting with everything I can think of lol. tbh I don't see why this doesn't work as I followed a worksheet exactly... and the worksheet does spoon feed you.

:/
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: What is wrong with this Linked List destructor?

Post by avansc »

pastebin the code in entirety and post the link.

edit: also, if you wanna use 0 that is perfectly fine. its the same thing. #define NULL (void*)0
Last edited by avansc on Mon Mar 29, 2010 4:11 pm, edited 1 time in total.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: What is wrong with this Linked List destructor?

Post by xiphirx »

StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: What is wrong with this Linked List destructor?

Post by avansc »

k, give me 5 min to check it out.

edit: btw, i think you are deleting a null pointer, infact im pretty sure.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: What is wrong with this Linked List destructor?

Post by xiphirx »

avansc wrote:
k, give me 5 min to check it out.

edit: btw, i think you are deleting a null pointer, infact im pretty sure.
That is what I think I am doing too, but I don't know why its trying to delete one...
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: What is wrong with this Linked List destructor?

Post by avansc »

xiphirx wrote:
avansc wrote:
k, give me 5 min to check it out.

edit: btw, i think you are deleting a null pointer, infact im pretty sure.
That is what I think I am doing too, but I don't know why its trying to delete one...
mmm, im not sure what happening, i'll have to look at it for a min longer.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
Milch
Chaos Rift Junior
Chaos Rift Junior
Posts: 241
Joined: Sat Jul 11, 2009 5:55 am
Programming Language of Choice: C++
Location: Austria, Vienna

Re: What is wrong with this Linked List destructor?

Post by Milch »

I'm not sure - but where's the Constructor for LinkedList?
Maybe you didnt set first to NULL?
Follow me on twitter!
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: What is wrong with this Linked List destructor?

Post by avansc »

Milch wrote:I'm not sure - but where's the Constructor for LinkedList?
Maybe you didnt set first to NULL?
no he has not destructor for his node, thats the problem
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: What is wrong with this Linked List destructor?

Post by xiphirx »

Milch wrote:I'm not sure - but where's the Constructor for LinkedList?
Maybe you didnt set first to NULL?

Code: Select all

LinkedList(){first = NULL; mySize = 0;}
its in there
no he has not destructor for his node, thats the problem
So I have to make a destructor for my node class? (is wondering why it wasn't mention in this worksheet :/)

EDIT: just in case, this is what my worksheet tells me to do...
1. Declare a NodePointer pointer ptr and set it to point to the first node in the list
2. Traverse the list and for each node do the following
a. set first to the next node after that pointed to by ptr (i.e. first = ptr->next)
b. use delete to deallocate the node pointed to by ptr (i.e. delete ptr)
c. set ptr equal to first
Last edited by xiphirx on Mon Mar 29, 2010 4:39 pm, edited 1 time in total.
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: What is wrong with this Linked List destructor?

Post by avansc »

xiphirx wrote:
Milch wrote:I'm not sure - but where's the Constructor for LinkedList?
Maybe you didnt set first to NULL?

Code: Select all

LinkedList(){first = NULL; mySize = 0;}
its in there
no he has not destructor for his node, thats the problem
So I have to make a destructor for my node class? (is wondering why it wasn't mention in this worksheet :/)
well i dont know where you got that worksheet. but its one of the worse implementations i have ever seen of a linked list.
are you allowed to implement your own? id be willing to help you if you are interested.

edit: this Larry R. Nyhoff, looks like an accomplished author. you sure you followed that thing right if it is infact the same Larry R. Nyhoff.
Last edited by avansc on Mon Mar 29, 2010 4:40 pm, edited 1 time in total.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: What is wrong with this Linked List destructor?

Post by xiphirx »

avansc wrote:
xiphirx wrote:
Milch wrote:I'm not sure - but where's the Constructor for LinkedList?
Maybe you didnt set first to NULL?

Code: Select all

LinkedList(){first = NULL; mySize = 0;}
its in there
no he has not destructor for his node, thats the problem
So I have to make a destructor for my node class? (is wondering why it wasn't mention in this worksheet :/)
well i dont know where you got that worksheet. but its one of the worse implementations i have ever seen of a linked list.
are you allowed to implement your own? id be willing to help you if you are interested.
I'm supposed to follow it :( but I can add a node destructor I think... also, read my edited post above for my instructions :s
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: What is wrong with this Linked List destructor?

Post by avansc »

well you are missing the last step in the instructions
also you have the NodePointer in the wrong place.

Code: Select all

LinkedList::~LinkedList()
{
	NodePointer ptr = first;
	while (first->next) 
	{
		first = ptr->next;
		delete ptr;
		ptr = first;
    }
	if (first == 0) cout << "List destroyed\n";
	else cout << "List not destroyed\n";
}
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: What is wrong with this Linked List destructor?

Post by xiphirx »

avansc wrote:well you are missing the last step in the instructions
also you have the NodePointer in the wrong place.

Code: Select all

LinkedList::~LinkedList()
{
	NodePointer ptr = first;
	while (first->next) 
	{
		first = ptr->next;
		delete ptr;
		ptr = first;
    }
	if (first == 0) cout << "List destroyed\n";
	else cout << "List not destroyed\n";
}

This is what I had before I started experimenting, and it does not work :(
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
Post Reply