Mail Archives: djgpp/1999/01/04/07:06:42
On Mon, 4 Jan 1999, Peter Karp wrote:
> I would expect that it gives an arbitrary value and increases this
> value *each round*, because an auto variable (not initalized by
> default - this means not initalized at linking time and not
> initialized at the function call, until a specific value is assigend
> to that var. Is this right?) will have an arbitrary value at the
> beginning of the function call
This is correct. However, zero is one possible case of an ``arbitrary
value'', and since your program is very simple, the place on the stack
where the automatic variable is allocated is not reused between calls.
So it retains its last value.
> The trace shows 10 times 256. Why is this happening here, while
> running the program always seem to output 1 2 3 4 5 6 7 8 9 10, which
> seems scary to me too?
When you are running under a debugger, the program is loaded into a
different place in memory, so you see different contents. It is a known
fact that buggy programs with uninitialized variables behave under a
debugger differently.
If you *really* want to understand the difference, you will have to look
at the machine code generated by the compiler. (You haven't even said
what switches did you pass to the compiler.)
Anyway, why would you want to explore the area which is explicitly said
to be ``undefined behavior''? Uninitialized automatic variables should
not be used, period. Just stay away from them, and you will keep your
sanity longer ;-).
- Raw text -