delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1994/09/20/08:34:52

Subject: Re: strange linker behavior
To: S_Eckart AT lis DOT e-technik DOT tu-muenchen DOT de (Stefan Eckart)
Date: Tue, 20 Sep 1994 12:49:20 +0200 (MET DST)
From: Henrik Storner <storner AT olicom DOT dk>
Cc: djgpp AT sun DOT soe DOT clarkson DOT edu

> int delay;
> 
> main()
> {
>   printf("%d\n",delay);
> }
> 
> I would have expected to get a 0 as output, and in fact this was the case
> until v1.11m5. The current version prints a large negative number...

NEVER rely on the value of uninitialized variables! I don't know about
the ANSI standard, but K&R gives the following rules for initialization
of variables (2nd. ed, p. 85):

"In the abscence of explicit initialization, external and static variables
 are guaranteed to be be initialized to zero; automatic and register variables
 have undefined (i.e. garbage) initial values.

 Scalar variables may be initialized when they are defined, byt following
 the name with an equals sign and an expression:" ...

So the 'delay' variable is uninitialized, and the output is therefore random.
Indeed, changing the declaration in your sample program to

	static int delay;

produces '0' as output, with DJGPP 1.12.maint2.
-- 
Henrik Storner        | "Man is the best computer we can put aboard a space-
(storner AT olicom DOT dk)   |  craft ... and the only one that can be mass produced 
System Engineering    |  with unskilled labor."
Olicom Denmark        |                                   Wernher von Braun

- Raw text -


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