Order of destruction of static locals w.r.t. globals

Discussion in 'C++' started by Davlet Panech, Nov 10, 2006.

  1. Is this well-formed?

    #include <string>
    #include <iostream>

    std::string &foo() {
    static std::string s("abc");
    return s;
    }

    struct bar {
    ~bar() {
    std::cout << foo() << std::endl;
    }
    };

    bar b1;
    bar b2;

    int main() {
    return 0;
    }


    In gcc the static local gets destroyed before the second global (b2), is
    this behavior correct?

    Thanks,
    D.
     
    Davlet Panech, Nov 10, 2006
    #1
    1. Advertising

  2. Davlet Panech wrote:
    > Is this well-formed?
    >
    > #include <string>
    > #include <iostream>
    >
    > std::string &foo() {
    > static std::string s("abc");
    > return s;
    > }
    >
    > struct bar {
    > ~bar() {
    > std::cout << foo() << std::endl;
    > }
    > };
    >
    > bar b1;
    > bar b2;
    >
    > int main() {
    > return 0;
    > }
    >
    >
    > In gcc the static local gets destroyed before the second global (b2),
    > is this behavior correct?


    Well-formedness has nothing to do with behaviour. Your program is
    well-formed as written.

    Your program most likely has undefined behavoiur according to 3.6.3/2.
    Although that paragraph describes a call to a function containing
    a static local object which has been destroyed, and your case calls
    such function for the first time (the object hasn't been created),
    since the creation is attempted during termination, I'd say it's UB.

    IOW, don't do that.

    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, Nov 10, 2006
    #2
    1. Advertising

  3. Davlet Panech

    Salt_Peter Guest

    Davlet Panech wrote:
    > Is this well-formed?
    >
    > #include <string>
    > #include <iostream>
    >
    > std::string &foo() {

    std::cerr << "foo()\n";
    > static std::string s("abc");
    > return s;
    > }
    >
    > struct bar {
    > ~bar() {
    > std::cout << foo() << std::endl;

    std::cerr << "~bar()\n";
    foo();
    > }
    > };
    >
    > bar b1;
    > bar b2;
    >
    > int main() {
    > return 0;
    > }
    >
    >
    > In gcc the static local gets destroyed before the second global (b2), is
    > this behavior correct?
    >
    > Thanks,
    > D.


    Its definetly undefined behaviour. If you replace the static
    std::string with a type S, static, it looks like the following (as you
    already pointed out):

    bar()
    bar()
    ~bar()
    foo()
    S()
    ~S()
    ~bar()
    foo()

    The fix is place the bar objects in main() since then at least you
    control their lifetimes.

    bar()
    bar()
    ~bar()
    foo() <- S's ctor is invoked here
    S()
    ~bar()
    foo()
    ~S()
     
    Salt_Peter, Nov 10, 2006
    #3
  4. Salt_Peter wrote:
    > [..]
    > The fix is place the bar objects in main() since then at least you
    > control their lifetimes.


    You don't know that. I am sure that the whole point for the OP to
    ask his question was that b1 and b2 _have_ to be static.

    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, Nov 10, 2006
    #4
  5. Victor Bazarov wrote:
    > Salt_Peter wrote:
    >> [..]
    >> The fix is place the bar objects in main() since then at least you
    >> control their lifetimes.

    >
    > You don't know that. I am sure that the whole point for the OP to
    > ask his question was that b1 and b2 _have_ to be static.
    >
    > V


    Thanks for both of your replies; naturally, the real-life program that
    causes problems for me is more complex than my original post, with
    inter-dependent global objects some of whose dtor's indirectly go
    through static local declarations, I can't fix that easily :(

    D.
     
    Davlet Panech, Nov 10, 2006
    #5
    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. Giles Brown

    Question about "exec in globals, locals"

    Giles Brown, Jul 4, 2003, in forum: Python
    Replies:
    2
    Views:
    360
    Adrien Di Mascio
    Jul 4, 2003
  2. tedsuzman
    Replies:
    2
    Views:
    7,094
    Michel Claveau, résurectionné d'outre-bombe inform
    Jul 21, 2004
  3. Ted
    Replies:
    1
    Views:
    470
    Duncan Booth
    Jul 22, 2004
  4. Olivier Sessink
    Replies:
    0
    Views:
    388
    Olivier Sessink
    Feb 24, 2005
  5. Victor Bazarov
    Replies:
    11
    Views:
    752
    James Kanze
    Dec 9, 2009
Loading...

Share This Page