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

查看:293
本文介绍了int var = 1; void main(){int i = i; }}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的面试问题:

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

i 分配后?它真的是编译器依赖或者它只是未定义?我的g ++对cygwin似乎一直给我0。



谢谢

解决方案>

i 有一个不确定的值,因为它没有被初始化。所以不仅是编译器依赖,而是依赖于在该内存位置发生的任何事情。局部范围中的变量未在C ++中初始化。



我假设在顶部你的意思是 int i,而不是 int var = 1; = 1;



局部范围 i



更具体地说,C ++ 03标准的第3.3.1-1节:


一个名称的声明点是
,紧跟在它的完整的
声明符(第8节)之后,
b初始化程序(如果有),除非在下面注明

[例如:

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

这里,第二个x被初始化为自己的(不确定)值。 ]







在旁注中,我不认为这是一个非常好的面试问题,因为它与知道一些关于语言的一些不清楚的事实有关,这些语言不会说明你的编码体验。


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

解决方案

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.

这篇关于int var = 1; void main(){int i = i; }}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆