Thread: Doubly Linked List Deletion Issue

  1. #16
    Registered User
    Join Date
    May 2007
    Posts
    15
    Quote Originally Posted by iMalc
    Right, you don't see what I mean at all. What I mean is that none of your pointers or counters are being set to NULL or zero before adding items to the list. That means that they would begin with a random address. Okay your real program more likely has them initialised, but we can't tell that from a mocked-up code snippet. You need to do your best to make sure that what you post is what you have actually been running. Setting the NodeCount back to zero afterwards is also important because otherwise you can't reuse the list properly.
    Alright, I see. No, my original code didn't initialize them either, I didn't really have any reason for them to begin in a certain memory location. As long as I am keeping the value of NodeStart and NodeEnd preserved things should be ok right? Or should I still initialize them. And yeah, I fixed the NodeCount reset error, just a careless mistake.

    Quote Originally Posted by iMalc
    Yes your other names aren't really that bad, once you correct your spelling of destruct.
    de·con·struct(dkn-strkt)
    tr.v. de·con·struct·ed, de·con·struct·ing, de·con·structs
    1. To break down into components; dismantle.

    It isn't a spelling error, just used a different word at the time, and it stuck.

    Quote Originally Posted by iMalc
    I figured as much, but when posting a snippet that reproduces a problem it is best to trim out as much irrelevant stuff as possible. Not just for our benefit, but because it may also help you to more clearly see the problem.
    *sighs* Sorry.. After eliminating several hundred lines of unrelated code, variables, and definitions, I left a stray var. Oh well.

    Quote Originally Posted by iMalc
    Really?! I'm intriegued as to how any home-baked list could be more flexible that a std::list! Care to elaborate?
    Well, I am using my LL code to attach different types of objects to each node which may themselves contain anything from strings to longs, to links to entirely different sublists.

    I guess I could probably use it by just storing pointers to each newly created object in the list and then referencing them that way... But I would rather have this worked out because now I am curious... but it seems no one knows why it is behaving in such a strange way..

  2. #17
    Registered User
    Join Date
    May 2007
    Posts
    15
    Ok, I wrote up a simple little program to use std::list but it shares the *exact* same problem as my code, except this takes up more memory, and doesn't create the nodes as fast... I am totally at a loss here.

    iMalc, I'll try to stay away from using unconventional words in my function names, it is probably not a good idea.

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <list>
    
    using namespace std;
    
    struct Node
    {
    	string handle;
    };
    
    list <Node*> LIST;
    
    void PushBackNode()
    {
    	LIST.push_back(new Node);
    }
    
    void PushBackNodes(unsigned long n)
    {
    	for(n;n>0;n--)
    	{
    		PushBackNode();
    	}
    }
    
    int main()
    {
    	cout << "Creating nodes..." << endl;
    	PushBackNodes(5);
    	cout << " - " << endl;
    	getchar();
    	cout << "clearing nodes..." << endl;
    	LIST.clear();
    	cout << " - " << endl;
    	getchar();
    	return 0;
    }
    This code eats up around 670MB vs. my 434MB... and creates the nodes slightly slower, however the .clear() function "works" faster than my hombrew function, but at least mine releases 85&#37; percent of the memory instead of 0%.

    btw, the 'string handle' is just for comparing sizes, I did the same to my previous code which it where I'm getting my numbers.
    Last edited by josephjah; 07-21-2007 at 11:10 PM. Reason: Added a thing or two...

  3. #18
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    list <Node> LIST;

  4. #19
    Registered User
    Join Date
    May 2007
    Posts
    15
    robwhit, I'm not sure that I understand the reasoning behind that.

    What I'm thinking, If I use:
    Code:
    list <Node*> LIST;
    and
    Code:
     LIST.push_back(new Node);
    I will be creating a new object and then storing a pointer to it... however, now that I am experimenting with std:list I've realized I don't know how to access a particular element without writing a function specifically for that purpose which takes the size of the object and any padding and skips through memory manually....

    Any way I can access an element located within the list? sort of like an array or vector?

  5. #20
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you call new Node
    but do not call delete on it, when you clear your list
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #21
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    if you use it like you did then you're making a list of pointers. you allocate with new, but then that allocated stuff doesn't get deleted when you call clear. I think (I could be wrong) that it should be like this:
    Code:
    Node *ti = new Node;
    LIST.push_back(*ti);

  7. #22
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by josephjah View Post
    Alright, I see. No, my original code didn't initialize them either, I didn't really have any reason for them to begin in a certain memory location. As long as I am keeping the value of NodeStart and NodeEnd preserved things should be ok right? Or should I still initialize them. And yeah, I fixed the NodeCount reset error, just a careless mistake.
    Yeah you really do have to make sure that they are initialised to NULL at some point. How you do that depends on the real code of yours though. If they're member variables, use a constructor initializer list. If gobals, assign NULL in the declaration. I was once told strictly to always initialise my variables, even though I was using a language and compiler that always did that. It's advice you come to share after a while.

    de&#183;con&#183;struct(dkn-strkt)
    tr.v. de&#183;con&#183;struct&#183;ed, de&#183;con&#183;struct&#183;ing, de&#183;con&#183;structs
    1. To break down into components; dismantle.

    It isn't a spelling error, just used a different word at the time, and it stuck.
    Oh of course, that is a real word, I almost forgot. Still in the context of C++ code it is fingernails on a blackboard for many people. Much safer to stick with 'destruct' all the time.
    *sighs* Sorry.. After eliminating several hundred lines of unrelated code, variables, and definitions, I left a stray var. Oh well.
    Sorry, didn't mean to be hard on you. Some of us just like to be thorough.
    Well, I am using my LL code to attach different types of objects to each node which may themselves contain anything from strings to longs, to links to entirely different sublists.

    I guess I could probably use it by just storing pointers to each newly created object in the list and then referencing them that way... But I would rather have this worked out because now I am curious... but it seems no one knows why it is behaving in such a strange way..
    You can use a std::list in two ways.
    Each node contains a pointer to your struct and a pointer to the next/prev nodes, or
    Each node is essentially a structure that contains your struct as one of the members and the next/prev pointers as other members.
    The later means that the memory allocation/deallocation is handled entirely with/by the list itself. The former means you allocate what the node points to and you delete it later.
    Usually you would use the second method and store the object directly in the list. E.g. std::list<string> for a list of strings.
    But if the list doesn't own the object (maybe this is just a sublist and they ALL appear in a different list anyway), then you use std::list<string*>

    Still it's a good experience to write your own list, so I'd suggest keeping at it until it is sorted anyway. Well done sticking at it so far.
    Last edited by iMalc; 07-23-2007 at 12:17 AM. Reason: Sorry, wrote first where I meant second
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #23
    Registered User
    Join Date
    May 2007
    Posts
    15
    iMalc, Thanks. I've been fiddling with std::list for a little while now and things still aren't working. However I have found the root of my original problem. (Well, sorta)

    Currently my main dev computer is running Vista, and so I tried the code on two other computers running XP, and it turns out they are releasing the memory perfectly, there is no leak in XP... So, I have decided to just put up with it and do my hardcore memory-intensive testing on my XP machine.

    So, thanks everyone for your help. I only wish there was another solution to the problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  2. WIP Borland C Doubly linked list
    By BCWarrior in forum C Programming
    Replies: 41
    Last Post: 09-25-2007, 12:06 AM
  3. doubly linked list error.
    By noob2c in forum C++ Programming
    Replies: 12
    Last Post: 09-01-2003, 10:49 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM