C++ Vector of objects

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mathlete
    New Member
    • Aug 2007
    • 1

    C++ Vector of objects

    Hello,

    I'm just starting to learn C++, coming from a Java / bit of C background. I have some questions about how vectors of objects and pointers work in C++.

    Say I want to create a vector of SomeClass objects, and the SomeClass constructor takes one argument. However, SomeClass is not copyable, so I can't do something like:
    Code:
    vector<SomeClass> someVector;
    for (int i=0; i<args.size(); i++){
        someVector.push_back( SomeClass(args[i]) )
    }
    So instead, I used a temporary variable and stored pointers inside the vector instead:
    Code:
    vector<SomeClass *> someVector;
    for (int i=0; i<args.size(); i++){
        SomeClass temp(args[i]);
        someVector.push_back(&temp);
    }
    However, all the pointers in someVector ended up pointing to temp(args[args.size()-1]). I've resolved this by using boost::shared_p tr instead, but I wanted to understand why the above code didn't work as expected. I thought that in each iteration of the for loop, a new temp will get created, and a reference to it will be stored in someVector (so that it won't garbage collected or w/e since there's still a reference to it). The same thing also seems to happen when I make a vector of a struct...

    From general Googling, I get the feeling that in general, it's probably a good idea to get into the habit of storing pointers instead of actual objects and structs in vectors - so I should probably get used to using vectors of shared_ptr?

    Also, any other general comments, tips, common gotchas, etc. will be greatly appreciated (trying to learn all I can!).

    Thanks!
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by mathlete
    Code:
    vector<SomeClass *> someVector;
    for (int i=0; i<args.size(); i++){
        SomeClass temp(args[i]);
        someVector.push_back(&temp);
    }
    However, all the pointers in someVector ended up pointing to temp(args[args.size()-1]).
    Yep, 'temp' is just a local variable and no matter how many times your evaluate
    '&temp' it'll always be the address of that same local variable. Do this instead:

    Code:
    vector<SomeClass *> someVector;
    for (int i=0; i<args.size(); i++){
        SomeClass* temp= new SomeClass(args[i]);
        someVector.push_back(temp);
    }
    See the difference?

    kind regards,

    Jos

    Comment

    • Darryl
      New Member
      • May 2007
      • 86

      #3
      Originally posted by JosAH
      Yep, 'temp' is just a local variable and no matter how many times your evaluate
      '&temp' it'll always be the address of that same local variable. Do this instead:

      Code:
      vector<SomeClass *> someVector;
      for (int i=0; i<args.size(); i++){
          SomeClass* temp= new SomeClass(args[i]);
          someVector.push_back(temp);
      }
      See the difference?

      kind regards,

      Jos
      and since now you are using new, you can drop the temp and go back to your original design someVector.push _back(new SomeClass(args[i]))

      Comment

      • oscarin43
        New Member
        • Feb 2012
        • 1

        #4
        How to delete allocated memory

        Hi. Respect to the example. Could you please tell me the right way to delete all the memory allocated from this piece of code.

        i believe that i have to do something like this.

        vector<SomeClas s *> someVector;

        for (int i=0; i<args.size(); i++){
        SomeClass* temp= new SomeClass(args[i]);
        someVector.push _back(temp);

        delete temp; // Is necessary??
        }

        //and next;

        int VecSize = someVector.size ();
        for (int i=0; i<VecSize; i++)
        {
        delete someVector[i]; //Is correct?
        }

        thanks for your help

        Comment

        • abcde456
          New Member
          • Mar 2013
          • 1

          #5
          don't delete temp within assignment for loop

          //do all the work you wanted to do with someVector
          // then delete like you showed in the last forloop, before you go out of scope

          Comment

          Working...