Array of Linked Lists

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kardon33
    New Member
    • May 2007
    • 158

    Array of Linked Lists

    Alright what I have done is implemented a standard linked list class in C++ called Targets which contains holds the list of objects called target.

    That part is not a problem and everything seems to work find when I create a Targets object in main and add and delete targets.

    Then I created another class that is basically just to separate some of the source code into section. So that class is called my Tracker class which contains the Targets linked list object. But in that class I need to keep an array of 10 linked list objects.

    During the initialization of the Tracker class create the Targets objects like:

    for(int i=0;i<TRACK_LEN ;i++){
    allTargets[i] = new Targets();
    }

    and allTargets[] is defined in the header file like:
    Targets* allTargets[TRACK_LEN];



    The Problem is when I go to use the a Targets object from the allTargets[] I get a seg fault. from using the allTargets object like:
    allTargets[0]->count();


    I can fix it if I reinitialize the Targets object before I use it like:
    allTargets[0] = new Targets();
    allTargets[0]->count();

    but that will delete my data so that wont work, any ideas?
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    This would be easier with more of the source code but possibilities include

    Where you use allTargets you accidentally declare a local version of the variable hiding the member variable in Tracker.

    Somewhere in your Tracker class you accidentally assign zero values to your allTargets array.

    There is an error else where in your code stamping over that particular data, an out of bounds array access for example.


    Finally why have you implemented Targets when you could have just used the STL list and why do you have allTargets as a dynamically allocated array when you could have just used a vector?

    Comment

    • kardon33
      New Member
      • May 2007
      • 158

      #3
      Well I found where and how the error was occurring but havnt figured out how to fix it.

      My programs runs in a loop and at the start of each loop I add a new Targets object to the the allTargets[0] and push back all the other spots. Well the error seems to be in my assignment operator in the LL class.

      Assignment Operator is as fallows

      Targets Targets::operat or=(const Targets& other)
      {
      Head = other.Head;
      return *this;
      }



      To your last questions: No reason.

      What would be the proper way to do it.


      My Target object is:
      s
      truct target {
      // Set on initialize
      int Num; // Is just the num in the l.l.
      CvPoint Center; // Defines State Vector with speed.
      int width;
      int height;
      float Area;

      // Variables that are not set on start.
      float Direction;
      int Speed; // Defines State Vector with center.
      int Located;
      int LocatedTrack[10]; // Not used yet.
      CvPoint Track[10];

      target *Next;
      };

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Banfa asked why you are not using and STL list or vector for this. Why are you not? All of these problems have already been worked out in the C++ Standard Library.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Right well with that structure all you would need to do is remove the Next member and then have something like

          Code:
          #include <list>
          #include <vector>
          using namespace std;
          ...
          
          list<target> myListOfTargets;
          
          // You can define the type to make things easier
          
          typedef list<target> Targets;
          
          // The allTargets becomes
          
          vector<Targets> allTargets(TRACK_LEN);
          Targets::operat or= has a few problems, you should always protect against self assignment and whatever Head originally pointed to is just lost. If it was allocated from the heap that is a memory leak. In what you already have you would need to free what Head currently points to

          Code:
          Targets Targets::operator=(const Targets& other)
          {
              // If other is this object
              if (this = &other)
              {
                  // Do nothing return
                  return *this;
              }
          
              // Should be code here to release what head
              // currently points to to avoid memory leak
          
              Head = other.Head;
              return *this;
          }
          From your description it is not clear eactly what is meant to happen, this

          allTargets[0] = new Targets list
          allTargets[1] = allTargets[0]
          allTargets[2] = allTargets[1]
          ...
          allTargets[TRACK_LEN-2] = allTargets[TRACK_LEN-1]

          what was in allTargets[TRACK_LEN-1] is discarded

          or this

          allTargets[0] += new Targets list
          allTargets[1] - allTargets[TRACK_LEN-1] unchanged

          or something else.

          Depending on what you are trying to do a different container (rather than vector) might be best for allTargets.

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            P.S.

            vector reference
            list reference

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Originally posted by kardon33
              Targets Targets::operat or=(const Targets& other)
              {
              Head = other.Head;
              return *this;
              }
              This assignment operator is not making an assignment. That is, it is not making a copy of the other object. All it's doing the copying the head. Noe you have two Targets with the same head. You need to a) delete the elements in the this object, b) make a copy of the other list and attach it to the this object and c) do not allow an assignment of the this object to itself. If you do, the this object will delete itself in (a).

              Comment

              Working...