41

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; } 
8
  • 19
    You tagged both C and C++. Which did you compile? Commented Oct 1, 2015 at 20:43
  • 3
    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) Commented Oct 1, 2015 at 23:32
  • @Joakim: Interesting; thanks for the results. Are GCC and Clang within their rights, i.e. is this canonically undefined behaviour? Commented Oct 2, 2015 at 8:08
  • 1
    @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. Commented Oct 2, 2015 at 14:09
  • 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 Commented Oct 2, 2015 at 14:15

4 Answers 4

52

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

5

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

int x = x; 

in your program.

2 Comments

The OP doesn't seem to know whether they want an answer for C or for C++. The latter does support scope resolution.
@underscore_d, yes C++ supports and C doesn't.
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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.