function static variable expression

Discussion in 'C++' started by Gianni Mariani, May 7, 2004.

  1. What does the standard say about this code ...

    #include <iostream>

    const char * func( const char * v )
    {
    std::cout << "func called with v = " << v << "\n";
    return v;
    }

    void zoo( const char * v )
    {
    static const char * sv( func( v ) ); // What happens here on SECOND?

    std::cout << "zoo called with v=" << v << " sv=" << sv << "\n";
    }

    int main()
    {
    zoo( "FIRST" );
    zoo( "SECOND" );
    }

    gcc 3.4.0 and MS C++ 7.1 give me:

    func called with v = FIRST
    zoo called with v=FIRST sv=FIRST
    zoo called with v=SECOND sv=FIRST


    This means that the initializer expression for static variables is
    initialized only once. Tricky.

    Has anyone seen problems with compilers getting this wrong ?
     
    Gianni Mariani, May 7, 2004
    #1
    1. Advertising

  2. Gianni Mariani wrote:
    > ...
    > This means that the initializer expression for static variables is
    > initialized only once. Tricky.
    > ...


    Yes, that's how it is supposed to work. The initialization is not very
    tricky, BTW. The tricky part is destruction of local static objects with
    non-trivial destructor. The standard requires that their relative order
    of destruction in the reverse of their relative order of construction.
    Since the relative order of construction for such objects is only known
    at run-time, the implementation must keep track of this order at run-time.

    > Has anyone seen problems with compilers getting this wrong ?


    I haven't seen any problems with initialization. I've seen at least one
    compiler that got the order of destruction wrong.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, May 7, 2004
    #2
    1. Advertising

  3. Gianni Mariani

    Rolf Magnus Guest

    Gianni Mariani wrote:

    > This means that the initializer expression for static variables is
    > initialized only once.


    Yes, that's what static is all about in local variables. The variable is
    initialized only once, when the function is entered the first time.

    > Tricky.


    Not really. The implementation could simply keep some hidden extra
    variable for any function with local static objects that tells them
    whether those are already initialized or not.

    > Has anyone seen problems with compilers getting this wrong ?


    No.
     
    Rolf Magnus, May 7, 2004
    #3
  4. * Rolf Magnus <> schriebt:
    > Gianni Mariani wrote:
    >
    > > Tricky.

    >
    > Not really. The implementation could simply keep some hidden extra
    > variable for any function with local static objects that tells them
    > whether those are already initialized or not.


    See Andrey Tarasevich's reply in this same thread -- there is a tricky
    aspect, and some hidden extra variable like a bool does not suffice.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is top-posting such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, May 7, 2004
    #4
  5. Andrey Tarasevich wrote:
    > Gianni Mariani wrote:
    >
    >>...
    >>This means that the initializer expression for static variables is
    >>initialized only once. Tricky.
    >>...

    >
    >
    > Yes, that's how it is supposed to work. The initialization is not very
    > tricky, BTW. The tricky part is destruction of local static objects with
    > non-trivial destructor. The standard requires that their relative order
    > of destruction in the reverse of their relative order of construction.
    > Since the relative order of construction for such objects is only known
    > at run-time, the implementation must keep track of this order at run-time.
    >
    >
    >>Has anyone seen problems with compilers getting this wrong ?

    >
    >
    > I haven't seen any problems with initialization. I've seen at least one
    > compiler that got the order of destruction wrong.
    >


    The destruction would be very tricky with loading and unloading
    dll's/dso's ! The standard's requirement may even be unenforceable
    across dll's/dso's.

    Regarding the tricky thing which I miswrote, the issue that I was the
    "initializer expression" is evaluated "only once" is non-intuitive at
    first glance. I think it makes sense, but my colleagues shuddered when
    I showed them the code.
     
    Gianni Mariani, May 7, 2004
    #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. kookey
    Replies:
    3
    Views:
    20,778
    Andrew Thompson
    Aug 20, 2005
  2. Patrick Hoffmann
    Replies:
    3
    Views:
    2,851
    Christian Jan├čen
    Aug 8, 2003
  3. Marcin Vorbrodt

    Global static variable vs static method

    Marcin Vorbrodt, Sep 5, 2003, in forum: C++
    Replies:
    3
    Views:
    5,434
    Denis Perelyubskiy
    Sep 5, 2003
  4. Replies:
    2
    Views:
    527
    Jonathan Mcdougall
    Aug 6, 2007
  5. tomer
    Replies:
    0
    Views:
    2,051
    tomer
    Jul 28, 2011
Loading...

Share This Page