Date: Sun, 5 Oct 1997 22:30:57 -0700 (PDT) Message-Id: <199710060530.WAA06419@adit.ap.net> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" To: djgpp AT delorie DOT com From: Nate Eldredge Subject: Re: Bug in DJGPP? Precedence: bulk At 08:39 10/5/1997 -0400, A. Sinan Unur wrote: >Vic wrote: >> >> Liche wrote: >> > >> > Liche wrote: >> > >> > I am sorry, it displays 21 ten times if I use auto short count = 20;, >> > instead of 21 22 23... >> > Anyways, this is still a weird result. It seems that if I don't >> > initialize a variable to a value then it is static, even if i use auto >> > short count; ...... >> > Can anybody else reproduce this? >> yes, me. And indeed I find it weird. Cause if I say something like >> auto int x; then I print the value of X it will be something random. > >it is absolutely not weird. 'auto' is not a synonym for 'random variable >generator'. the whole point is the value of a non-static local variable >is not initialized, it is undefined. undefined does not mean every time >the variable is created, the compiler is going to spend extra effort >making sure that the value it points to contains some 'random' (whatever >you guys are using it to mean) value. the variable is not initialized. >the value it holds is undefined. therefore, it can be anything including >your birthday. Quite correct. The specific reason it's always being the same is that auto variables are allocated on the stack. Since you are calling your function from the same parent, with no other calls in between, it is at the same address every time. When you increment it once, you come back later and get the same number back again. This is coincidental and NOT TO BE DEPENDED ON!!! Nate Eldredge eldredge AT ap DOT net