vectors stl with pointers

Discussion in 'C++' started by berthelot samuel, Jul 30, 2003.

  1. hi all,
    I have a simple question. I use a stl vector of pointers on objects of
    my own class, say Person:
    vector<Person*> vectPerson;
    vectPerson.push_back(new Person("Sam"));

    when I call vectPerson.pop_back, do i need to take care of the pointer
    by calling delete or does the pop method handle this for me ?
    thx
    Sam
    berthelot samuel, Jul 30, 2003
    #1
    1. Advertising

  2. > when I call vectPerson.pop_back, do i need to take care of the pointer
    > by calling delete or does the pop method handle this for me ?

    you will have to delete it on your own!
    Patrik Stellmann, Jul 30, 2003
    #2
    1. Advertising

  3. "berthelot samuel" <> wrote:
    > I have a simple question. I use a stl vector of pointers on objects of
    > my own class, say Person:
    > vector<Person*> vectPerson;
    > vectPerson.push_back(new Person("Sam"));
    >
    > when I call vectPerson.pop_back, do i need to take care of the pointer
    > by calling delete or does the pop method handle this for me ?


    No, you will have to do it yourself. The pop methods just remove the
    pointer from the vector. The std::vector class cannot know if it has to
    delete the object that is being referenced by the pointer; depending on
    the situation it may or may not be the appropriate thing to do. Rather
    than using raw pointers, you may also opt for putting boost::shared_ptr
    of the boost library (http://www.boost.org/) in the vector. This way you
    don't have to worry about ownership issues.

    --
    Peter van Merkerk
    peter.van.merkerk(at)dse.nl
    Peter van Merkerk, Jul 30, 2003
    #3
  4. "berthelot samuel" <> wrote in message
    news:...
    > hi all,
    > I have a simple question. I use a stl vector of pointers on objects of
    > my own class, say Person:
    > vector<Person*> vectPerson;
    > vectPerson.push_back(new Person("Sam"));
    >
    > when I call vectPerson.pop_back, do i need to take care of the pointer
    > by calling delete or does the pop method handle this for me ?
    > thx
    > Sam


    No you have to do it one your own, also you have to keep track of copies of
    your vector.

    This is why you shouldn't use raw pointers in an STL class (or anywhere
    similar). Do yourself a favour and learn about smart pointers, will save you
    months of debugging tricky pointer problems. Have a look at the smart_ptr
    class from www.boost.org for instance, or get a good book.

    john
    John Harrison, Jul 30, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Aramini

    C++ STL valarrays vs. vectors

    Michael Aramini, Sep 2, 2003, in forum: C++
    Replies:
    10
    Views:
    13,119
    vrambati
    Nov 3, 2003
  2. Tom
    Replies:
    1
    Views:
    346
    Moonlit
    Dec 14, 2003
  3. Replies:
    3
    Views:
    693
    Shadowman
    Mar 26, 2008
  4. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    672
  5. Guest
    Replies:
    0
    Views:
    439
    Guest
    Sep 14, 2005
Loading...

Share This Page