使用可变用在不同的空间相同的名称 [英] Using a variable with the same name in different spaces

查看:78
本文介绍了使用可变用在不同的空间相同的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这code编译,但我在 Visual Studio中运行时错误:


  

运行时检查失败#3 - 变量X被不被初始化使用...


  INT X = 15;
诠释的main()
{
    INT X = X;
    返回0;
}

我不明白这种行为......在错误框当我点击继续程序恢复和X有一个损坏的内容(如而不是 -8556328 15 )。

为什么这code的工作没有问题,而int数组很好声明?

  const int的X = 5;
诠释的main()
{
     INT×〔X] = {1,2,3,4};
     返回0;
}


解决方案

X 在左侧定义 =

所以在 X [X] [X] 指的是全球性的,

,而在 X = X; X 隐藏全局 X ,并从自身初始化 - > UB

This code compiles, but I have a run time error in Visual Studio:

Run-time check failure #3 - the variable 'x' is being used without being initialized...

int x = 15;
int main()
{
    int x = x;
    return 0;
}

I don't understand that behavior... in the error box when I click continue the program resumes and x has a corrupted content (like -8556328 instead of 15).

Why does this code work without a problem, and the int array is well declared?

const int x = 5;
int main()
{
     int x[x] = {1,2,3,4};
     return 0;
}

解决方案

x is defined at the left of =.

so in x[x], [x] refer to the global one,

whereas in x = x;, x hides the global x and initializes from itself -> UB.

这篇关于使用可变用在不同的空间相同的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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