Why an uninitialized variable in C still produces an output

I am trying to understand why, although the local variable I was never initialized in this C program, many systems print 0 1 2 3 4 5 6 7 8 9 Can anyone explain why this is so? Any help is appreciated!

 void foo() { int i; printf("%d ", i++); } int main() { int j; for (j = 1; j <= 10; j++) foo(); } 
+3
source share
5 answers

The behavior is undefined. Statistics do not matter. This may be due to the layout and initialization of the stack, but it could be for any other reason.

For example, if:

  • It does not check if variables are initialized or not.
  • This is a simple stack machine.
  • The stack is initialized to 0.
  • The variable i is allocated on the stack, not as a register.
  • When a function is called, it does not initialize the stack.

In this case, i will refer to the same place on the stack every time, will start at 0 and the same place on the stack will increase by one each time.

+7
source

The variable i is created on the stack, and you did not initialize it. Therefore, it contains some trash value that you cannot predict. Fortunately, this garbage cost is initially 0 in your case. If you start another system, you will get a different set of values. Undefined behavior for sure.

As you are not doing anything except calling this function in a for loop, fortunately, this variable i destroyed and redistributes the same memory space every time. Thus, it retains its value by iterations.

On my system, this gives these values.

 2009288233 2009288234 2009288235 2009288236 2009288237 2009288238 2009288239 2009288240 2009288241 2009288242 
+3
source

This may be because a local variable in a function is repeatedly allocated and freed from the same memory block. That is why the output is repeatedly increased.

An integer value of zero is luck. It can be any other garbage cost.

The behavior is undefined and will not work on another system.

+2
source

Since this is a simple program, you are lucky that the i-th core uses i. Since this is not initialized, it will get the previous value.

However, it is undefined. You should compile it with warnings to select this error.

0
source

As others have noted, this behavior is undefined, so there is no solid answer.

However, you are probably right that the most common values ​​you get will be from 0 to 9. This will happen because the OS zeros out the memory occupied by the stack before your program receives it. foo() is the deepest call that has been made, so it uses memory that was not affected after the OS nullified it, so the first call it will probably still contain zero.

In each subsequent call, he is likely to occupy the same place on the stack, so at the beginning of each subsequent call he will probably start with the value at the end of the previous call.

Sine is undefined behavior, none of this is guaranteed at all, but yes, maybe it will be at least a little more likely.

0
source

Source: https://habr.com/ru/post/981854/


All Articles