Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

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;
}
share|improve this question
13  
You tagged both C and C++. Which did you compile? –  underscore_d 23 hours ago
2  
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 20 hours ago
    
@Joakim: Interesting; thanks for the results. Are GCC and Clang within their rights, i.e. is this canonically undefined behaviour? –  underscore_d 12 hours ago
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. –  Jirka Hanika 6 hours ago
    
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 6 hours ago

4 Answers 4

up vote 29 down vote accepted

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.

share|improve this answer

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.

share|improve this answer

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

int x = x;

in your program.

share|improve this answer
6  
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 23 hours ago
    
I think that it compiles I checked it in ideone.com with the C compiler... –  Aminos 23 hours ago
    
@underscore_d, yes C++ supports and C doesn't. –  Adi 23 hours ago

You didn't describe your code correctly: probably your main function is static. A static function of any class can't use the non-static members of that class

class Program
{
    int x = 4;
    static int y = 5;

    static void Main(string[] args)
    {
        Console.WriteLine(x.ToString()); // <- compiler error: x not static
        Console.WriteLine(y.ToString()); // <- Ok, y is also static
    }
}
share|improve this answer
11  
This doesn't look like C or C++ to me... –  Stig Hemmer 11 hours ago
3  
I do not see the C# tag on the question, which this answer appears to assume. –  Snowman 8 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.