473,387 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

function static variable expression


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 ?
Jul 22 '05 #1
4 1557
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

Jul 22 '05 #2
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.

Jul 22 '05 #3
* Rolf Magnus <ra******@t-online.de> 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?
Jul 22 '05 #4
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.

Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
6
by: Michael B Allen | last post by:
I want to initialize a static variable to a "random" value like: static void * get_key(struct dnsreq *req) { static uint16_t next_txnid = (uint32_t)req & 0xFFFF; But gcc gives me an error: ...
7
by: Csaba Gabor | last post by:
I feel like it's the twilight zone here as several seemingly trivial questions are bugging me. The first of the following three lines is a syntax error, while the last one is the only one that...
4
by: alex | last post by:
I am so confused with these three concept,who can explained it?thanks so much? e.g. var f= new Function("x", "y", "return x * y"); function f(x,y){ return x*y } var f=function(x,y){
2
by: psujkov | last post by:
Hi everybody, let us have some class A with const static int variable var with compile-time well-known value. let us have some function f(), which has return type of A. and let us have some...
5
by: Radu | last post by:
Hi. In a repeater I have as ItemTemplate the following, among others, and everything works great: <asp:SqlDataSource ID="LocationSqlDataSource" SelectCommand="SELECT blah-blah-blah"...
12
by: Googy | last post by:
Hi!! Can any one explain me the meaning of following notations clearly : 1. typedef char(*(*frpapfrc()))(); frpapfrc f; 2. typedef int (*(arr2d_ptr)()); arr2d_ptr p; 3. typedef int...
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.