这是我的面试问题:
int var = 1;
void main()
{
int i = i;
}
Run Code Online (Sandbox Code Playgroud)
i分配后的价值是多少?它真的是编译器依赖的还是仅仅是未定义的?我在cygwin上的g ++似乎总是给我0.
谢谢
Bri*_*ndy 15
i具有不确定的值,因为它未初始化.因此,它不仅依赖于编译器,而且还取决于该内存位置发生的任何情况.本地范围中的变量未在C++中初始化.
我认为而不是int var = 1;在顶部你的意思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:
Run Code Online (Sandbox Code Playgroud)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.
| 归档时间: |
|
| 查看次数: |
2362 次 |
| 最近记录: |