vector of pointers.

Discussion in 'C++' started by Dave, May 1, 2007.

  1. Dave

    Dave Guest

    A quick question:

    vector <T *> test_vec;
    T testT;
    test_vec.push_back(&testT);

    T * testT2p = new T;
    test_vec.push_back(testT2p);

    Now at the end do I need to explicitly delete the vector (test_vec).
    How should I make sure that the memory allocation is ok?

    Thanks,
    Dave, May 1, 2007
    #1
    1. Advertising

  2. Dave wrote:
    > A quick question:
    >
    > vector <T *> test_vec;
    > T testT;
    > test_vec.push_back(&testT);
    >
    > T * testT2p = new T;
    > test_vec.push_back(testT2p);
    >
    > Now at the end do I need to explicitly delete the vector (test_vec).


    Only the elements that were explicitly 'new'ed.

    > How should I make sure that the memory allocation is ok?


    There is no other non-implementation-specific way except to keep
    some kind of flag to distinguish between the ones you got from 'new'
    and the others.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, May 1, 2007
    #2
    1. Advertising

  3. Dave

    Guest

    On May 1, 1:05 pm, "Victor Bazarov" <> wrote:
    > Dave wrote:
    > > A quick question:

    >
    > > vector <T *> test_vec;
    > > T testT;
    > > test_vec.push_back(&testT);

    >
    > > T * testT2p = new T;
    > > test_vec.push_back(testT2p);

    >
    > > Now at the end do I need to explicitly delete the vector (test_vec).

    >
    > Only the elements that were explicitly 'new'ed.
    >
    > > How should I make sure that the memory allocation is ok?

    >
    > There is no other non-implementation-specific way except to keep
    > some kind of flag to distinguish between the ones you got from 'new'
    > and the others.
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask


    To make things strait - you don't have to 'delete' your
    test_vec since it's a stack variable. You do have to
    worry about proper memory deallocation though
    every time you have a collection of pointers.

    One simple solution is to use std::vector<boost::shared_ptr<T> >.
    The Boost shared pointer allows you to specify the destruction
    function (that defaults to normal delete if not specified)
    Wrap your stack/global object pointers with empty function,
    and leave heap-allocated objects be 'delete'd.
    That's almost the same as keeping the flag as above,
    but moves your effort to vector insert time (when you know
    where the object came from) instead of vector remove time
    (when you don't.)
    --
    Nikolai
    , May 1, 2007
    #3
  4. Dave

    James Kanze Guest

    Dave wrote:
    > A quick question:


    > vector <T *> test_vec;
    > T testT;
    > test_vec.push_back(&testT);


    > T * testT2p = new T;
    > test_vec.push_back(testT2p);


    > Now at the end do I need to explicitly delete the vector (test_vec).


    If it is a local variable, no, but you'll have to delete
    testT2p. Formally, only after the destructor of the vector has
    been called.

    > How should I make sure that the memory allocation is ok?


    Use the Boehm collector.

    Otherwise, something like:

    vector< T* > test_vec ;
    T testT ;
    test_vec.push_back( &testT ) ;
    std::auto_ptr< T > testT2p( new T ) ;
    test_vec.push_back( testT2p.get() ) ;

    will handle this particular case.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, May 2, 2007
    #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. pmatos
    Replies:
    6
    Views:
    23,727
  2. Replies:
    8
    Views:
    1,891
    Csaba
    Feb 18, 2006
  3. Javier
    Replies:
    2
    Views:
    544
    James Kanze
    Sep 4, 2007
  4. zl2k
    Replies:
    27
    Views:
    1,553
    Francesco S. Carta
    Sep 7, 2010
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    654
Loading...

Share This Page