views:

235

answers:

3

This is an interview question that I had:

int var = 1;
void main()
{
    int i = i;
}

What is the value of i after assignment? It is really compiler dependent or is it just undefined? My g++ on cygwin seems to give me 0 all the time.

Thanks

+12  A: 

i has an indeterminate value because it is not initialized. So not only is it compiler dependent but it is dependent on whatever happens to be in that memory location. Variables in local scope are not initialized in C++.

I assume instead of int var = 1; at the top you meant int i = 1;.

The local scope i will still be used because the point of declaration for a variable is immediately after its declarator and before its initializer.

More specifically, Section 3.3.1-1 of the C++03 standard:

The point of declaration for a name is immediately after its complete declarator (clause 8) and before its initializer (if any), except as noted below. [Example:

int x = 12;
{ int x = x; }

Here the second x is initialized with its own (indeterminate) value. ]


On a side note I don't think this is a very good interview question because it is related to knowing some obscure fact about the language which doesn't say anything about your coding experience.

Brian R. Bondy
While I agree it's a terrible interview question, it potentially might say *something* about your coding experience. For example, it's something you'd need to consider if you ever tried to write a macro that tried to declare its own variables (and then came to the conclusion to use inline functions instead).
jamesdlin
@jamesdlin: Maybe but I wouldn't deduce this since you can't tell the difference whether they just happen to know that fact or not.
Brian R. Bondy
They're looking to check your analytical skills...Folks, not everything is a cut-and-dry question. They want to see you work through a problem and that you can come up with a solution, they want to see how deep your knowledge goes. You might answer something like "That depends on the compiler" or walk them through the stack operations and explain where the value will come from, or you could say it's defined out of scope and is therefore assigned to an un-initialized value... You could even say "Oh, it's going to be random memory data," just show you can think through the problem.
@user257493: There's not much to work though, other than knowing the key from the C++03 spec that says that the point of declaration of a name is directly after its declarator and before its initializer. You can also figure it out easily if you were given a compiler to work with but I doubt the OP was.
Brian R. Bondy
+3  A: 

If you really meant the code you said, it's undefined, since int i = i; is the same as just doing int i;, which leaves it uninitialized. However, you probably meant:

int i = 1;
void main() {
    int i = i;
}

i is still undefined in the local scope, although the question is at least slightly more interesting. The local i shadows the global one as soon as it's defined, so by the time the assignment happens the local definition already exists, and the right-hand side i refers to the i defined within main, not the global one. If it didn't removing the global int i = 1; would cause a compile error, as int i = i; would refer to an i that doesn't exist yet

Michael Mrozek
A: 

Why should i have any value at all here, even undefined? The assignment does nothing, it could be optimized out entirely.

Loren Pechtel
It's an initialization by the way not an assignment.
Brian R. Bondy
But i goes unused and the function can't have side effects. Thus it could be removed entirely.
Loren Pechtel
I think for interview purposes you'd ignore that sort of thing
Michael Mrozek