function static variable expression

G

Gianni Mariani

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 ?
 
A

Andrey Tarasevich

Gianni said:
...
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.
 
R

Rolf Magnus

Gianni said:
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.

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.
 
A

Alf P. Steinbach

* Rolf Magnus said:
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.
 
G

Gianni Mariani

Andrey said:
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.




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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top