question on the order in which objects created on the stack getdestroyed

Discussion in 'C++' started by Yan, Nov 18, 2008.

  1. Yan

    Yan Guest

    Can I rely on the order in which destructors for objects 'a' and 'b'
    are called in the attached code or it's up to the compiler to decide?

    Thank you.

    -------------------
    class A {};
    class B {};

    void f() {
    A a;
    B b;
    }
    -------------------
    Yan, Nov 18, 2008
    #1
    1. Advertising

  2. Yan wrote:
    > Can I rely on the order in which destructors for objects 'a' and 'b'
    > are called in the attached code or it's up to the compiler to decide?
    >
    > Thank you.
    >
    > -------------------
    > class A {};
    > class B {};
    >
    > void f() {
    > A a;
    > B b;
    > }
    > -------------------


    Yes. In C++ static and automatic objects are destroyed in the reverse
    order of their creation.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Nov 18, 2008
    #2
    1. Advertising

  3. Jeff Schwab wrote:
    > Andrey Tarasevich wrote:
    >> Yan wrote:
    >>> Can I rely on the order in which destructors for objects 'a' and 'b'
    >>> are called in the attached code or it's up to the compiler to decide?
    >>>
    >>> Thank you.
    >>>
    >>> -------------------
    >>> class A {};
    >>> class B {};
    >>>
    >>> void f() {
    >>> A a;
    >>> B b;
    >>> }
    >>> -------------------

    >>
    >> Yes. In C++ static and automatic objects are destroyed in the reverse
    >> order of their creation.

    >
    > Given that they are in the same translation unit.


    No. The global order or destruction is always the reverse of the global
    order of construction, whatever the latter might be. It is true that the
    order of construction [of static objects] is not specified across
    translation units, but once some (any) global construction order took
    place during the actual execution of the program, the order of
    destruction is required to be the exact reverse of it. Globally, i.e.
    across all translation units.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Nov 19, 2008
    #3
  4. Jeff Schwab wrote:
    >>
    >> No. The global order or destruction is always the reverse of the
    >> global order of construction, whatever the latter might be. It is true
    >> that the order of construction [of static objects] is not specified
    >> across translation units, but once some (any) global construction
    >> order took place during the actual execution of the program, the order
    >> of destruction is required to be the exact reverse of it. Globally,
    >> i.e. across all translation units.

    >
    > D'oh. I stand corrected. Do you know where the standard covers this?
    > I'm having a little trouble finding it.


    I believe "3.6.3 Termination" covers it in the very first paragraph.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Nov 19, 2008
    #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. e
    Replies:
    1
    Views:
    366
  2. jimjim
    Replies:
    12
    Views:
    1,891
    Ron Natalie
    Jun 3, 2005
  3. lovecreatesbeauty
    Replies:
    43
    Views:
    1,298
    Keith Thompson
    Feb 6, 2006
  4. lovecreatesbeauty
    Replies:
    2
    Views:
    577
  5. Stephan Kämper
    Replies:
    2
    Views:
    232
    Stephan Kämper
    Jan 18, 2004
Loading...

Share This Page