Thread: runtime error...

  1. #1
    Registered User Mr_Jack's Avatar
    Join Date
    Oct 2003
    Posts
    63

    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.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    If its just a simple single linked list, its

    Code:
    node * temp = head;
    while ( temp != NULL ) {
        node *next = temp->next;
        delete temp;
        temp = next;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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;
    	}
    }

  4. #4
    Registered User Mr_Jack's Avatar
    Join Date
    Oct 2003
    Posts
    63
    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?
    Last edited by Mr_Jack; 03-03-2004 at 09:16 PM.

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Code:
    	test.~string();
    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;
    }

  6. #6
    Registered User Mr_Jack's Avatar
    Join Date
    Oct 2003
    Posts
    63
    thanks a lot, I tried putting it in it's own scope and I didn't get an errors. Thanks a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM