在不同的空间中使用具有相同名称的变量

时间:2021-11-24 16:48:43

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

这段代码会编译,但是我在Visual Studio中有一个运行时错误:

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

运行时检查失败#3 -变量“x”正在使用而没有初始化…

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).

我不理解那种行为……在错误框中,当我单击continue时,程序恢复,x有一个已损坏的内容(比如-8556328而不是15)。

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

为什么这个代码没有问题,而且int数组被很好地声明了?

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

4 个解决方案

#1


51  

x is defined at the left of =.

x在=的左边定义。

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

在x[x]中,[x]表示全局变量,

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

而在x = x中,x隐藏全局x并初始化自身-> UB。

#2


35  

When you declare a new variable, its name becomes visible right here

当你声明一个新的变量时,它的名字就会在这里显示出来

int x =
//     ^- there

because it is at that point the variable is fully declared, and as such; its name means something. At this point in time any other (previously declared variable) in a surrounding scope will be hidden.

因为在这一点上变量是完全声明的;它的名字的意思是什么。此时,周围范围中的任何其他(先前声明的变量)都将被隐藏。

#3


5  

There is no scope resolution operator in C, so you may not be able to use

在C中没有范围解析操作符,因此您可能无法使用

int x = x;

in your program.

在你的计划。

#4


1  

please use SRO( Scope resolution operator ::) to tell compiler which x is real x in your mind. As user defined names are mangled( Names are decorated) something like this to avoid ambiguity at it's level, these are just names used by compiler that best suits it

请使用SRO(Scope resolution operator:::)告诉编译器您心目中的哪个x是真实的x。由于用户定义的名称被分解(名称被修饰),为了避免在它的层次上出现歧义,这些只是编译器使用的最适合它的名称

int x = 15;// Real name = gui_x
int main()
{
    int x = x;// lui_x
    return 0;
}

In this way run-time will know which version you are using but to avoid ambiguity it expects from you to use specific names. Sometimes above problem arise where you don't know that you are using already used names. For this C++ has created SRO.
Now in case of array x is address & not integer that stores something, that's why compiler didn't jumbled. You need to write

通过这种方式,运行时将知道您正在使用哪个版本,但为了避免使用特定名称所期望的模糊性。有时,在您不知道您正在使用已使用的名称时,会出现上述问题。为此c++创建了SRO。如果数组x是地址而不是整数,那么编译器就不会出错。你需要写

namespace abc //now all global variables are belongs to this ns abc
int x = 15;// Real name = gui_x
int main()
{
int x = abc::x;// lui_x
return 0;
}

#1


51  

x is defined at the left of =.

x在=的左边定义。

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

在x[x]中,[x]表示全局变量,

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

而在x = x中,x隐藏全局x并初始化自身-> UB。

#2


35  

When you declare a new variable, its name becomes visible right here

当你声明一个新的变量时,它的名字就会在这里显示出来

int x =
//     ^- there

because it is at that point the variable is fully declared, and as such; its name means something. At this point in time any other (previously declared variable) in a surrounding scope will be hidden.

因为在这一点上变量是完全声明的;它的名字的意思是什么。此时,周围范围中的任何其他(先前声明的变量)都将被隐藏。

#3


5  

There is no scope resolution operator in C, so you may not be able to use

在C中没有范围解析操作符,因此您可能无法使用

int x = x;

in your program.

在你的计划。

#4


1  

please use SRO( Scope resolution operator ::) to tell compiler which x is real x in your mind. As user defined names are mangled( Names are decorated) something like this to avoid ambiguity at it's level, these are just names used by compiler that best suits it

请使用SRO(Scope resolution operator:::)告诉编译器您心目中的哪个x是真实的x。由于用户定义的名称被分解(名称被修饰),为了避免在它的层次上出现歧义,这些只是编译器使用的最适合它的名称

int x = 15;// Real name = gui_x
int main()
{
    int x = x;// lui_x
    return 0;
}

In this way run-time will know which version you are using but to avoid ambiguity it expects from you to use specific names. Sometimes above problem arise where you don't know that you are using already used names. For this C++ has created SRO.
Now in case of array x is address & not integer that stores something, that's why compiler didn't jumbled. You need to write

通过这种方式,运行时将知道您正在使用哪个版本,但为了避免使用特定名称所期望的模糊性。有时,在您不知道您正在使用已使用的名称时,会出现上述问题。为此c++创建了SRO。如果数组x是地址而不是整数,那么编译器就不会出错。你需要写

namespace abc //now all global variables are belongs to this ns abc
int x = 15;// Real name = gui_x
int main()
{
int x = abc::x;// lui_x
return 0;
}