开发者

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

开发者 https://www.devze.com 2023-01-05 10:39 出处:网络
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 se

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 t开发者_如何学Pythono give me 0 all the time.

Thanks


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.


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


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消