-
runtime error...
There's some mistake I made is my function to delete all the nodes in my linked list and I don't know what it is. I get some sort of memory error. I'll just post the code for the function because you don't really need to see all of the class and stuff...
Code:
string::~string()
{
node * temp1 = head;
node * temp2 = head->next;
while(temp1 || temp2)
{
delete temp1;
if(temp2)
if(temp2->next)
temp1 = temp2->next;
delete temp2;
if(temp1->next)
temp2 = temp1->next;
}
}
I get the error if the variable head points to a node and when head->next = 0. It won't finish even one iteration of the loop, the error occurs somewhere in the block for the if statement "if(temp2)". I know that even if temp2 = 0 that it executes the code inside that if statement. Any help would be greatly appreciated.
-
If its just a simple single linked list, its
Code:
node * temp = head;
while ( temp != NULL ) {
node *next = temp->next;
delete temp;
temp = next;
}
-
your code seems unnecessarily complex for the destructor of a simple linked list where the terminal node is defined by the next pointer being NULL (or 0 or whatever). see if this works for you:
Code:
list::~list()
{
node * temp1 = head;
node * temp2;
while(temp1 != 0)
{
temp2 = temp1;
temp1 = temp1->next;
delete temp2;
}
}
-
I tried your way of doing it salem and I still get a memory error. If you would please look at my entire program to see what I'm doing wrong, I would be very, very grateful.
Code:
#include <iostream>
using std::cout;
using std::endl;
//using std::cin;
class string
{public:
//protected:
struct node
{
node * next;
char c;
node() : next(0) {}
};
unsigned long * len;
node * current;
node * head;
void addnode();
inline void gonext();
inline void gohead();
//public:
string();
virtual ~string();
};
void string::addnode()
{
if(!current->next)
current->next = new node;
else
{
node * newnode = new node;
newnode->next = current->next;
current->next = newnode;
}
}
inline void string::gonext()
{
if(current->next)
current = current->next;
}
inline void string::gohead()
{
current = head;
}
string::string()
{
head = new node;
current = head;
}
string::~string()
{
node * temp;
current = head;
while(current)
{
temp = current->next;
delete current;
current = temp;
cout << "foo" << endl;
}
}
int main()
{
string test;
test.~string();
return 0;
}
i commented out the...what do you call what private/protected/public are?...things...so that I could cout some protected members to see if they are what I expect them to be. They are. My while loop in the destructor seems to run twice and start a third iteration before it crashes when I only have a single node. It crashes when I delete current on the third iteration. Thank you guys for your help so far!
hang on, I've been doing some testing and some weird stuff happens. I used cin at the very end of my main function to stop my program from quitting as soon as it stops executing. My program made it to the cin with no problems. I entered some gibberish to make it continue, and THEN I got my memory error. I have absolutely no clue why this is. Could you please tell me what you think it might be?
-
You shouldn't be calling the destructor like this. The destructor will be called automatically when the object goes out of scope. In this case, after the return 0 in main. If you want to call the destructor earlier than that (e.g. to see debugging information), then make a separate block like this:
Code:
int main()
{
{
string test;
}
return 0;
}
-
thanks a lot, I tried putting it in it's own scope and I didn't get an errors. Thanks a lot.