5

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

Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175
user382499
  • 587
  • 3
  • 8
  • 11
  • 15
    Seems like a terrible interview question. I think knowing obscure language oddities is a skill that is too often tested (and not very important in the long run). I'd rather have someone that would go ask or look it up than someone that knew everything such as this. – Chimmy Jul 04 '10 at 02:01
  • 2
    It won't even compile, you need at least `int main()`. – Benjamin Bannier Jul 04 '10 at 02:02
  • 9
    Why do you have `int var = 1;` at the top? – Brian R. Bondy Jul 04 '10 at 02:03
  • 9
    Undefined. That's what this word is used for. Like, a komodo dragon might climb out and eat your LCD screen. The interviewer might in fact be looking to start a discussion with you about how a compiler technology (or lay programmer) might implement techniques that catch these type of bugs, etc. – rwong Jul 04 '10 at 02:06
  • @Brian I think it meant to say `int i = 1;`. – 5ound Jul 04 '10 at 08:05
  • @rwong: Komodo warans eat meat, not electronic. Otherwise you're right, though. – sbi Jul 04 '10 at 09:53
  • @sbi: on a more useful note, VC++ 2008 Express in Debug mode does detect this condition, at compile time, by emitting a call to _RTC_UninitUse. – rwong Jul 04 '10 at 10:46
  • They ask it because they want to see you have a deeper understanding and the ability to think outside of a given question. It's important to know your programmer is a thinker, not just someone who does lines of code all day. – Incognito Jul 06 '10 at 02:09
  • People used to write `int i=i;` on purpose as a way to avoid a compiler warning about the uninitialized variable. Even today, in gcc, -Winit-self is not enabled by -Wall. – Marc Glisse Jan 17 '13 at 21:44

5 Answers5

15

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
  • 339,232
  • 124
  • 596
  • 636
  • 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 Jul 04 '10 at 09:33
  • @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 Jul 04 '10 at 13:09
  • 1
    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. – Incognito Jul 06 '10 at 02:37
  • @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 Jul 06 '10 at 02:49
  • In the lastest C++1y draft this becomes [undefined behavior for the int case](http://stackoverflow.com/questions/23415661/has-c1y-changed-with-respect-to-the-use-of-indeterminate-values-and-undefined). – Shafik Yaghmour May 02 '14 at 20:45
  • I don't think this is a terrible question--knowing that the value is undefined is important. I have seen subtle bugs due to a variable being referenced before initialization where the data normally left in memory was harmless. – Loren Pechtel May 23 '21 at 23:43
3

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
  • 169,610
  • 28
  • 168
  • 175
1

This causes undefined behavior because you read from i before its lifetime starts.

Firstly:

[basic.life]/1.2

... The lifetime of an object ... begins when:

— its initialization (if any) is complete

The initialization of i is not complete when you read from i, because you need to know the value to finish the initialization.

Next:

[basic.life]/7.1

... before the lifetime of an object has started but after the storage which the object will occupy has been allocated ... any glvalue that refers to the original object may be used but only in limited ways. ... The program has undefined behavior if:

— the glvalue is used to access the object

Reading from i does "access the object", hence UB.

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
-1

I had the same interview question and I can confirm that it was indeed int var = 1;

Which implies that that does make some difference. I would run the code but I'm not at my PC right now.

I agree that questions like these are daft for interviews. By analogy it's like if someone is interviewing for a job as a chef and the question is "If you fed someone a spoonful of e coli, drizzled with typhoid, how sick would they get?". You're unlikely to ever need that knowledge in the job.

Mijin
  • 125
  • 1
  • 9
-1

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

Loren Pechtel
  • 8,945
  • 3
  • 33
  • 45