Thread: Linked list troubles...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    11

    Linked list troubles...

    You may remember me asking before about if linked lists would be better suited for my game programming needs. So, I've begun work on my own linked list to manipulate my game's particle engine.

    However, I've run into difficulty. The system appears to function perfectly -- it creates particles and deletes them flawlessly. But, after a certain number of particles are created/destroyed (or so it seems), the game crashes with no errors or warnings or anything of the sort. I'm quite positive that the linked list is the source of this problem.

    Does anyone know off the top of their head what I could be doing wrong with the linked lists? Is this somesort of memory leak, or could I be deleting/adding nodes wrong?

    If nobody is able to solve this, I will followup with some code.

    Thanks!

  2. #2
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    Use print statements to debug!!!!!

    print statements are the absolute best way to debug code at any time.

    Print out all of your variables at key points in time. If you see an inconsitency with that the variables SHOULD be, then there is your problem

    If you still can't figure anything out, post code.
    My Website

    "Circular logic is good because it is."

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    14
    Like DavidP said the best thing to do is have print statements after the creation of an entity in your Linked List to see what data is actually there.

    Also you can try using a debugger to see what the linked list contains as you go through your code. GDB was a blessing from God when I was doing Linked Lists.

    Off the top of my head:

    1) Maybe it is the data that you are feeding it which is somewhat corrupted.

    2) Make sure you properly check the special cases for removing, ex. Remove from Begening, remove from End, because those are a little tricky.

    other than that it is hard to figure out what is wrong without seeing some code.

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    11
    I already tried to use GDB to debug my program, but it oddly caused my computer to lock-up when the program would normally crash.

    Anyway, here is a (shortened) version of my linked list code (add/remove/process):

    Code:
    Particle *Head = new Particle;
    Head->next = NULL;
    Particle *Tail = Head;
    
    void Particle_Engine_Update()
    {
      Particle *tmp = Head;
      while(tmp != NULL && tmp->next != NULL)
      {
        tmp->next->Update();
        tmp->next->lifetime--;
        if(tmp->next->lifetime < 0)
        {
            Particle *corpse = tmp->next;
            Particle *tmp2;
            if(corpse == Head)
            {
                    tmp2 = Head;
                    Head = Head->next;
                    delete tmp2;
            }
            else if(corpse == Tail)
            {
                    tmp2 = Tail;
                    Tail = tmp;
                    Tail->next = NULL;
                    delete tmp2;
            }
            else
            {
                    tmp2 = tmp;
                    tmp2->next = corpse->next;
                    delete corpse;
            }
        }
        tmp = tmp->next;
      }
    }
    
    void Add_Particle()
    {
      Particle *tmp = new Particle;
      Tail->next = tmp;
      Tail = tmp;
    }
    Thanks!

  5. #5
    I am he who is the man! Stan100's Avatar
    Join Date
    Sep 2002
    Posts
    361
    Couldn't he (Gapless) use vectors instead?
    Stan The Man. Beatles fan

    When I was a child,
    I spoke as a child,
    I thought as a child,
    I reasoned as a child.
    When I became a man,
    I put childish ways behind me"
    (the holy bible, Paul, in his first letter to the Cor. 13:11)

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    11
    Originally posted by Stan100
    Couldn't he (Gapless) use vectors instead?
    Vectors? Mathematical vectors, like velocity and such? Or is this somesort of programming technique that I haven't had the fortune of coming across yet?

  7. #7
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I would assume he means vectors as in the stl container class
    (#include <vector> )
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  8. #8
    I am he who is the man! Stan100's Avatar
    Join Date
    Sep 2002
    Posts
    361

    Yep...

    Yeah, I mean the stl vector class.
    Someone correct me if I'm mistaken, but couldn't he use them instead.

    Here's a tutorial straight from this site:

    http://www.cprogramming.com/tutorial/stl/vector.html

    It doesn't go into much detail, but hopefully it'l give you enough info to google it
    Stan The Man. Beatles fan

    When I was a child,
    I spoke as a child,
    I thought as a child,
    I reasoned as a child.
    When I became a man,
    I put childish ways behind me"
    (the holy bible, Paul, in his first letter to the Cor. 13:11)

  9. #9
    Registered User
    Join Date
    Sep 2003
    Posts
    11

    Re: Yep...

    Originally posted by Stan100
    Here's a tutorial straight from this site:

    http://www.cprogramming.com/tutorial/stl/vector.html

    It doesn't go into much detail, but hopefully it'l give you enough info to google it
    Very nifty, indeed. This will make life much easier. Thanks!

  10. #10
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    Yes the standard template classes are all very nifty. I suggest you learn them all. They come quite in handy.
    My Website

    "Circular logic is good because it is."

  11. #11
    Isn't there a special STL thing called "list" as well that is better suited for linked lists?

  12. #12
    I am he who is the man! Stan100's Avatar
    Join Date
    Sep 2002
    Posts
    361

    Re: Re: Yep...

    Originally posted by Gapless
    Very nifty, indeed. This will make life much easier. Thanks!
    Always happy to help.
    Stan The Man. Beatles fan

    When I was a child,
    I spoke as a child,
    I thought as a child,
    I reasoned as a child.
    When I became a man,
    I put childish ways behind me"
    (the holy bible, Paul, in his first letter to the Cor. 13:11)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  3. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  4. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  5. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM