Uninitialized behavior of variables in C ++

I checked myself, I wrote a program like this

int main() {
 int i;
 cout << i;
 return 0;
}

      

I ran the program several times and the result was the same all the time, zero. I tried this in C and the result was the same.

But my tutorial says

If you do not initialize a variable defined inside the function, the value of the variable remains undefined. This means that the element takes on regardless of what value was previously at that location in memory.

How is this possible when the program always assigns a free memory location to a variable? How could this be something like zero (I'm assuming the default free memory value is zero)?

+3


source to share


3 answers


How is this possible when the program always assigns free memory to the location of the variable? How could this be something like scratch?

Let's see an example of a practical implementation.

Let's say it uses a stack to store local variables.

void
foo(void)
{
        int foo_var = 42;
}

void
bar(void)
{
        int bar_var;
        printf("%d\n", bar_var);
}

int
main(void)
{
        bar();
        foo();
        bar();
}

      



The completely broken code above shows the point. After we call foo, the specific place on the stack where foo_var was placed is set to 42. When we call bar, bar_var takes that exact location. Indeed, executing the code prints 0 and 42, indicating that bar_var cannot be used if it is not initialized.

It should now be clear that local variable initialization is required. But can the main thing be an exception? Is there anything that could play with the glass and give us a non-zero value as a result?

Yes. main is not the first function executed in your program . In fact, it takes tons to complete all tasks . Any of these jobs could use the stack and leave some non-zero on it. Not only can you not expect the same value on different operating systems, this can change dramatically on the very system you are using right now. Interested parties can use Google for the "dynamic linker".

Finally, the C standard doesn't even have a term stack. The space for local variables remains in the compiler. It might even cause random crap from what happened in this register. It really could be completely anything . In fact, if undefined behavior is triggered, the compiler is free to do whatever it wants. It can delete all of your porn, so be careful.

+8


source


If you do not initialize a variable defined inside a function, the value of the variable remains undefined.

This bit is true.

This means that the item takes on any previously stored value at that location in memory.

This bit is not.



This sometimes happens in practice, and you have to understand that getting zero or not zero is ideal for this theory for any given run of your program.

In theory, your compiler could actually assign a random seed to this integer if it wants to, so trying to rationalize this is completely pointless. But proceed as if we assumed that "the item takes on whatever value was previously stored at that location in memory" & hellip;

How can it be something and not zero (I'm assuming the default free memory is zero)?

Ok, this is what happens when you assume. :)

+4


source


Static variables and global variables are initialized to zero:

Global:
int a;  //a is initialized as 0

void myfunc(){
   static int x;     // x is also initialized as 0
   printf("%d", x);}

      

Where as non-static variables or automatic variables, that is, local variables are undefined (undefined usually means that it can do something. It can be zero, it could be the value that was there, it could crash the program) ... Reading them before assigning a value results in undefined behavior.

void myfunc2(){
   int x;        // value of x is assigned by compiler it can even be 0
   printf("%d", x);}

      

This mostly depends on the compiler, but in most cases this value is assumed to be 0

+1


source







All Articles