delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/01/04/07:06:42

Date: Mon, 4 Jan 1999 14:05:30 +0200 (IST)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
X-Sender: eliz AT is
To: djgpp AT delorie DOT com
Subject: Re: scope of auto vars and debugging problem
In-Reply-To: <369087e4.2704427@news.rrz.uni-koeln.de>
Message-ID: <Pine.SUN.3.91.990104135536.26097K-100000@is>
MIME-Version: 1.0
Reply-To: djgpp AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019