STL destruction question...

Discussion in 'C++' started by barcaroller, Apr 21, 2007.

  1. barcaroller

    barcaroller Guest

    When a STL container is destroyed, will the destructor of the individual
    objects be automatically called?
    barcaroller, Apr 21, 2007
    #1
    1. Advertising

  2. barcaroller

    Ian Collins Guest

    barcaroller wrote:
    > When a STL container is destroyed, will the destructor of the individual
    > objects be automatically called?
    >

    Yes, assuming they have one!

    --
    Ian Collins.
    Ian Collins, Apr 21, 2007
    #2
    1. Advertising

  3. barcaroller

    barcaroller Guest

    "Ian Collins" <> wrote in message
    news:...
    > barcaroller wrote:
    >> When a STL container is destroyed, will the destructor of the individual
    >> objects be automatically called?
    >>

    > Yes, assuming they have one!


    Does this also apply to object pointers (e.g. set<Class*>)? In this case I
    would create the object myself (using 'new Class') but I assume that, since
    I created it, I should destroy it. However, there's no reason why the STL
    container could not do that for me (by calling delete for each object).
    Would it?
    barcaroller, Apr 21, 2007
    #3
  4. barcaroller

    Mark P Guest

    barcaroller wrote:
    > "Ian Collins" <> wrote in message
    > news:...
    >> barcaroller wrote:
    >>> When a STL container is destroyed, will the destructor of the individual
    >>> objects be automatically called?
    >>>

    >> Yes, assuming they have one!

    >
    > Does this also apply to object pointers (e.g. set<Class*>)? In this case I
    > would create the object myself (using 'new Class') but I assume that, since
    > I created it, I should destroy it. However, there's no reason why the STL
    > container could not do that for me (by calling delete for each object).
    > Would it?
    >


    It will not and, if you consider it for a moment, you'll probably agree
    that it should not.
    Mark P, Apr 21, 2007
    #4
  5. barcaroller

    Andre Kostur Guest

    "barcaroller" <> wrote in
    news:f0blum$j15$:

    >
    > "Ian Collins" <> wrote in message
    > news:...
    >> barcaroller wrote:
    >>> When a STL container is destroyed, will the destructor of the
    >>> individual objects be automatically called?
    >>>

    >> Yes, assuming they have one!

    >
    > Does this also apply to object pointers (e.g. set<Class*>)? In this
    > case I would create the object myself (using 'new Class') but I assume
    > that, since I created it, I should destroy it. However, there's no
    > reason why the STL container could not do that for me (by calling
    > delete for each object). Would it?


    Follow the logic rigidly. The destructors of the contained object are
    called. The contained object is a Class*. The destructor for a Class* (or
    any other pointer for that matter) is a no-op. You may wish to consider
    using some sort of smart pointer class (but specifically not std::auto_ptr)
    as the contained object. This way the contained object is a smart_ptr
    <Class>, and thus ~smart_ptr<Class>() would be invoked. Or you could loop
    over the container and delete each pointer by hand.
    Andre Kostur, Apr 21, 2007
    #5
  6. barcaroller

    Gavin Deane Guest

    On 21 Apr, 01:30, "barcaroller" <> wrote:
    > "Ian Collins" <> wrote in message
    >
    > news:...
    >
    > > barcaroller wrote:
    > >> When a STL container is destroyed, will the destructor of the individual
    > >> objects be automatically called?

    >
    > > Yes, assuming they have one!

    >
    > Does this also apply to object pointers (e.g. set<Class*>)? In this case I
    > would create the object myself (using 'new Class')


    That's one way of obtaining a Class*. Another way is to take the
    address of an existing Class object - which may or may not have
    dynamic storage duration (i.e. may or may not need delete'ing).

    > but I assume that, since
    > I created it, I should destroy it.


    Absolutely.

    > However, there's no reason why the STL
    > container could not do that for me (by calling delete for each object).


    STL containers could have been designed that way. But such behaviour
    would rule out the possibility of storing pointers to anything other
    than dynamically allocated memory. The code below would be broken -
    something of a restriction on the way STL containers could be used.

    #include <vector>

    void foo()
    {
    int i = 42;
    int* pi = &i;
    std::vector<int*> v;
    v.push_back(pi);
    }

    > Would it?


    Nope. The library designers opted not to impose the above restriction
    on client code. You were right in your first assumption.

    If you are storing pointers to dynamically allocated objects, you can
    make your life easier by storing smart pointers that manage the delete
    automatically. Boost has examples and I believe some are included in
    std::tr1 if your compiler supports it.

    Gavin Deane
    Gavin Deane, Apr 21, 2007
    #6
  7. barcaroller

    James Kanze Guest

    On Apr 21, 2:30 am, "barcaroller" <> wrote:
    > "Ian Collins" <> wrote in message


    > news:...


    > > barcaroller wrote:
    > >> When a STL container is destroyed, will the destructor of the individual
    > >> objects be automatically called?


    > > Yes, assuming they have one!


    (Formally, all objects have destructors. Some destructors are
    trivial, however.)

    > Does this also apply to object pointers (e.g. set<Class*>)?


    Of course. The pointer gets destroyed. (Of course, the
    destructor of a pointer is trivial, so nothing much really
    happens.)

    The library does not persue pointers. Luckily, because in a lot
    of cases, the set<Class*> is used for navigation, and there are
    still a lot of other pointers to the objects when the set gets
    destructed.

    > In this case I
    > would create the object myself (using 'new Class') but I assume that, since
    > I created it, I should destroy it.


    Correct.

    > However, there's no reason why the STL
    > container could not do that for me (by calling delete for each object).
    > Would it?


    Only that it would break most common uses of sets of pointers.
    It's really fairly rare for a container to have ownership of a
    non-value object.

    --
    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, Apr 21, 2007
    #7
    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. ian douglas
    Replies:
    0
    Views:
    1,826
    ian douglas
    Aug 19, 2003
  2. Gavin Williams
    Replies:
    0
    Views:
    3,610
    Gavin Williams
    Jul 22, 2004
  3. Jeff

    TimerTask destruction

    Jeff, Aug 19, 2004, in forum: Java
    Replies:
    4
    Views:
    830
  4. johny smith

    question about object destruction

    johny smith, May 9, 2004, in forum: C++
    Replies:
    4
    Views:
    325
    Andrey Tarasevich
    May 10, 2004
  5. mblome
    Replies:
    3
    Views:
    4,697
    Old Wolf
    Jun 14, 2005
Loading...

Share This Page