TheCPUWizard, did I get this right:
http://www.codeguru.com/forum/showpo...9&postcount=51
If not, what the *&%! is the answer. :D
Printable View
TheCPUWizard, did I get this right:
http://www.codeguru.com/forum/showpo...9&postcount=51
If not, what the *&%! is the answer. :D
How aboutCode:v[i] = ++i;
That is PART of the answer. The second part of the answer has to deal with how the computer detects if statics are initialized. I dont have the spec with my, but it is (as one would assume) a "one-shot" deal.Quote:
Originally Posted by Zaccheus
Therefore the first time the static is referenced (or at some prior point) the assignment (initialization, or construction) must happen once. However if the static is referred to during this process it does NOT happen again.
Many people give the wrong answer that it is a recursive call that will stack crash....
I replied there... http://www.codeguru.com/forum/showth...=1#post1739503
Sorry for leaving you hanging...
Hmm, confirmed. CPUWiz is correct. Looked into the standards today just to confirm/refresh my memory. [basic.start.init - 3.6.2/1] :(
The value would be 0 and is well defined. Here the variable with static storage duration is initialized twice. Static initialization happens either to be via zero initialization or constant initialization (if a constant is used). Anything else is dynamic initialization and static initialization happens before any dynamic initialization. There is no constant expression involved, so zero initialization for the static variable happens and it gets the value 0. The function call then forms the dynamic initialization part, wherein, X is dynamically initialized with the statically initialized value of X i.e. 0. And hence, X is 0.
Pasting the code below for reference:Code:static int X = f(); //dynamic initialization via the function call.
//static initialization/zero initialization already has happened before the function call
int f() { return X; } //effectively returns 0.
int main()
{
cout << X << end;
}
So ...
andCode:#include <iostream>
int f();
static int X = f();
int f() { return X + 1; }
int main()
{
cout << X << end;
}
would be even more fun!Code:#include <iostream>
int f();
static int X = f();
int f() { return X++; }
int main()
{
cout << X << end;
}
Well, they both are undefined. Actually, if you read a variable twice in an expression where you also write it, the result is undefined.Quote:
Originally Posted by JohnW@Wessex
Btw, I wonder what happened about the interview. :rolleyes:
Your link doesn't work.Quote:
Originally Posted by Ejaz
http://www.research.att.com/~bs/bs_f...aluation-order
For some reason I had thought that the right hand side of the assignment is always evaluated before the left hand side of the assignment, in which case the result would have been well defined.Code:v[ i ] = i++;
Yes, when I thought about it some more it makes sense.Quote:
Originally Posted by Ejaz
Interestingly Visual Studio implements
v[i] = i++;
and
v[i] = ++i;
exactly as my initial gut reaction assumed they did.
Deliberate choice by Microsoft? Who knows.
I did read a comment on another forum that in C# these are well defined.
Does that mean that in C# the right hand side is always evaluated before the left hand side (or is it perhaps the other way around)?Quote:
Originally Posted by JohnW@Wessex
Wanders off to the C# forum ...
It wasn't on this site.Quote:
Originally Posted by Zaccheus
You sure? I think he posted this in the C# forum
http://www.codeguru.com/forum/showthread.php?t=457023
I didn't assume it was, I just went there to ask the question on this site. :)Quote:
Originally Posted by JohnW@Wessex
It was in one of the comments here.
Not having ever coded in C# I have no knowledge on this aspect.