40
votes

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;
}
4
You tagged both C and C++. Which did you compile? - underscore_d
Some interesting facts: gcc 4.8.4, compiles and this program can be run with -Wall -Wextra -pedantic turned on. clang 7.0.0 compiles it, and can be run as is. However if printf("%d\n", x); is added after int x=x; (I guess any actual usage of x), the compiler gives the more friendly warning: warning: variable 'x' is uninitialized when used within its own initialization [-Wuninitialized]. gcc still compiles and runs it even with the printf and printed 0. However running the program through valgrind gives Conditional jump or move depends on uninitialised value(s) - Joakim
@Joakim: Interesting; thanks for the results. Are GCC and Clang within their rights, i.e. is this canonically undefined behaviour? - underscore_d
@underscore_d - C++ doesn't require any diagnostics on uninitialized variables. And the compiler is free to optimize away a variable altogether, especially if it is never really used after the assignment. In fact, "undefined behavior" means that the compiler can do whatever it wishes. - Jirka Hanika
Yup, I know what UB means, just wanted to check that the standard defined (or rather, omits to define) these particular cases as UB. Thanks for the info - underscore_d

4 Answers

51
votes

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.

35
votes

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.

5
votes

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

int x = x;

in your program.

1
votes

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

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

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;
}