Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > function static variable expression

Reply
Thread Tools

function static variable expression

 
 
Gianni Mariani
Guest
Posts: n/a
 
      05-07-2004

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 ?


 
Reply With Quote
 
 
 
 
Andrey Tarasevich
Guest
Posts: n/a
 
      05-07-2004
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

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      05-07-2004
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.

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      05-07-2004
* Rolf Magnus <(E-Mail Removed)> 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?
 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      05-07-2004
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Variable argument function as a parameter of a variable argument function AikidoGuy C Programming 11 11-21-2011 10:43 PM
error: (infix expression) for formal "din" is not a globally static expression tomer VHDL 0 07-27-2011 11:43 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM
How to call a non static function from a static function bhattacharjeesoft@gmail.com C++ 2 08-06-2007 11:03 AM



Advertisments