3

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();
}
Joe Crawley
  • 725
  • 6
  • 18
  • 1
    On my system , it gives `134513713 134513714 134513715 134513716 134513717 134513718 134513719 134513720 134513721 134513722`. So , it may or may not by 0 1 2 etc. – axiom Dec 02 '12 at 06:31
  • 4
    Every answer attributes this to luck. You must be a really lucky guy :) – asheeshr Dec 02 '12 at 06:33
  • Yea, I'm starting to pick up on that, maybe I just know the right people :) – Joe Crawley Dec 02 '12 at 06:38
  • My hand-made C++ compiler erased my hard-disk 10 times. It think my compiler done it well according to the standard! – masoud Feb 14 '15 at 13:54

5 Answers5

7

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.

MByD
  • 135,866
  • 28
  • 264
  • 277
3

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
Coding Mash
  • 3,338
  • 5
  • 24
  • 45
2

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.

asheeshr
  • 4,088
  • 6
  • 31
  • 50
0

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.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
0

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.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111