Joe Crawley
Joe Crawley

Reputation: 725

Why an uninitialized variable in C still produces output

I'm trying to figure out why even though the local variable i is never intialized in this C program, many systems will print out 0 1 2 3 4 5 6 7 8 9 Can someone explain why this is? Any help is appreciated!

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

Upvotes: 3

Views: 351

Answers (5)

Coding Mash
Coding Mash

Reputation: 3346

Variable i is created on stack and you not initialized it. So it contains some garbage value which you cannot predict. Luckily, that garbage value is initially 0 in your case. If ran on another system, you would get a different set of values. The behaviour is undefined for sure.

As you are not doing anything except calling that function in the for loop, luckily again, that variable i gets destroyed and reallocated same memory space each time. So it retains its value across iterations as well.

On my system it gives these values.

2009288233
2009288234
2009288235
2009288236
2009288237
2009288238
2009288239
2009288240
2009288241
2009288242

Upvotes: 3

Jerry Coffin
Jerry Coffin

Reputation: 490178

As others have noted, it's undefined behavior, so there's no solid answer.

That said, you're probably right that the most common values you'd get would be 0 through 9. This would happen because the OS zeroed out the memory being occupied by the stack before your program received it. foo() represents the deepest call that's been made, so it's using memory that hasn't been touched after the OS zeroed it, so on the first call, it's fairly likely to still contain zero.

On each subsequent call, it's likely to occupy the same spot on the stack, so at the beginning of each subsequent invocation, it's likely to start with the value it had at the end of the previous invocation.

Sine it is undefined behavior, none of this is guaranteed at all, but yes, it's probably at least a bit more likely than not.

Upvotes: 0

Ed Heal
Ed Heal

Reputation: 60007

As this is a simple program you are lucky that the same memory location is used of i. Since this is not initialised it will pick up the previous value.

However this is undefined. Should compile it with the warnings to pick this bug up.

Upvotes: 0

asheeshr
asheeshr

Reputation: 4114

It could be because the local variable in the function is repeatedly being allocated and freed from the same memory block. This is why the output is repeatedly incremented.

The intial value being zero is luck. It could have been any other garbage value.

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

Upvotes: 2

MByD
MByD

Reputation: 137362

The behavior is undefined. The statistics are irrelevant. It might be because of the layout and the initialization of the stack, but it might be for any other reason as well.

For example, assuming:

  1. There is no check if variables are initialized or not.
  2. This is a simple stack machine.
  3. The stack is initialized to 0.
  4. The variable i is allocated on the stack and not as a register.
  5. When a function is called, it does not initialize the stack.

In such case i will refer to the same place on the stack each time, will start as 0 and the same place on the stack will be incremented each time by one.

Upvotes: 7

Related Questions