comp.lang.ada
 help / color / mirror / Atom feed
From: Jeffrey Creem <jeff@thecreems.com>
Subject: Re: Uninitialized variables
Date: Sat, 18 Mar 2006 09:36:24 -0500
Date: 2006-03-18T09:36:24-05:00	[thread overview]
Message-ID: <lfite3-fv3.ln1@newserver.thecreems.com> (raw)
In-Reply-To: <441C13DB.91C219FE@fakeaddress.nil>

Gautier wrote:
> Here (trying to to sum up), 3 problems I see with the tactic of
> initializing everything:
> 
>  - useless initializations (i.e. dummy values rewritten later) take
> time and usually _hurt_ performance (think to number crunching with
> huge objects, or frequently used functions with local variables)
>  - useless initializations introduce meaningless code lines
>  - [Dirk] useless initializations prevent detecting bugs that can be
> detected without these initializations (they can be detected by
> combining the Initialize_Scalars pragma and the validity checks)
> 
> My rule is rather to initialize _only_ variables you can give a
> meaningful values.

This is also the approach I follow. It has the added benefit that some 
compilers can now give you warnings about reading from it before you 
assign to it and thus help you find the bug. If one does the typical 
initialize everything to 0 or 'first or something like that then you can 
expect no help from the compiler.

Now in reality, compilers vary in their ability to provide useful 
warnings in this area.

GNAT does a reasonably good job of balancing real warnings in this case 
against false warnings.

Another compiler I use takes a different approach where it seems to warn 
in a lot more cases and thus ends up with a lot more false positives.
It is probably not that bad of an approach if you used this compiler 
from the beginning but with lots of lecagy code the signal to noise 
ratio of these warnings is so poor that I have not really found an 
effective way to make use of the warnings.


Consider the following toy code:

with Text_IO;
procedure Toy is

   I : Integer;
   I_Set : Boolean := False;
   Should_We_Set_I : Character;
   J : Integer;

begin

   Text_Io.Get(Should_We_Set_I);

   if Should_We_Set_I = 'y' then
     I_Set := True;
     I := 1;
   end if;


   if I_Set then
     Text_Io.Put_Line(Integer'Image(I));  -- This is ok
     Text_IO.Put_Line(Integer'image(J));  -- This is bad
   end if;

   J := 1;

end Toy;


GNAT Warns on the line that says This is bad but not on the line that 
says this is ok.

Another compiler I use warns on both (Not posting other compiler here 
only because I have not tested this exact code on it and am making this 
assertion based on similar real code).


Obviously, unless one uses someting like polyspace, a simple compiler 
can't be expected to detect all of these path flow type cases.


The important point here is that if one had a convention that all 
variables should be initialized, no compiler could tell you that you 
were doing something wrong on the "this is bad line".





  reply	other threads:[~2006-03-18 14:36 UTC|newest]

Thread overview: 94+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-03-13 19:58 private types ada_student
2006-03-13 20:27 ` Mark Lorenzen
2006-03-13 21:05   ` Pascal Obry
2006-03-13 21:07   ` ada_student
2006-03-13 21:45     ` Simon Wright
2006-03-14  4:51 ` Jeffrey R. Carter
2006-03-14  7:44   ` Brian May
2006-03-14  8:25     ` Ludovic Brenta
2006-03-14  8:47     ` Alex R. Mosteo
2006-03-17  4:33     ` Justin Gombos
2006-03-17  5:17       ` Brian May
2006-03-17 22:50         ` Justin Gombos
2006-03-18  1:17         ` Randy Brukardt
2006-03-18  2:17           ` Justin Gombos
2006-03-21  0:08             ` Randy Brukardt
2006-03-18  8:39           ` Uninitialized variables (was: Re: private types) Dirk Craeynest
2006-03-18 14:06             ` Gautier
2006-03-18 14:36               ` Jeffrey Creem [this message]
2006-03-21  0:22             ` Randy Brukardt
2006-03-21  0:38             ` Randy Brukardt
2006-03-18 12:06           ` private types Martin Dowie
2006-03-18 12:47           ` Robert A Duff
2006-03-17  7:40       ` Maciej Sobczak
2006-03-17 16:41         ` Frank J. Lhota
2006-03-17 23:36         ` Justin Gombos
2006-03-18  1:32           ` Randy Brukardt
2006-03-18  3:21             ` Handling invalid objects Justin Gombos
2006-03-18  7:35               ` Jeffrey R. Carter
2006-03-18 16:10                 ` Justin Gombos
2006-03-19 11:00                   ` Simon Wright
2006-03-20 23:57                   ` Randy Brukardt
2006-03-22  2:06                     ` Justin Gombos
2006-03-22  5:23                       ` tmoran
2006-03-22  8:48                         ` Dmitry A. Kazakov
2006-03-22  9:24                           ` Maciej Sobczak
2006-03-22 11:05                             ` Dmitry A. Kazakov
2006-03-22 16:42                               ` Maciej Sobczak
2006-03-22 18:06                                 ` Stefan Lucks
2006-03-23 13:20                                 ` Dmitry A. Kazakov
2006-03-18  8:57               ` Jacob Sparre Andersen
2006-03-19 19:07                 ` Dr. Adrian Wrigley
2006-03-20 15:25                   ` Robert A Duff
2006-03-19 22:06               ` Brian May
2006-03-20 21:17                 ` Jeffrey R. Carter
2006-03-20 23:44               ` Randy Brukardt
2006-03-22  1:27                 ` Justin Gombos
2006-03-18  9:20           ` private types Dmitry A. Kazakov
2006-03-17 13:18       ` Robert A Duff
2006-03-17 23:44         ` Justin Gombos
2006-03-18  9:24           ` Dmitry A. Kazakov
2006-03-18 12:56           ` Robert A Duff
2006-03-18 15:06             ` Justin Gombos
2006-03-19  9:35               ` Martin Krischik
2006-03-19 14:52                 ` Peter C. Chapin
2006-03-19 15:08                   ` Björn Persson
2006-03-19 16:34                     ` Martin Krischik
2006-03-20  9:57                       ` Maciej Sobczak
2006-03-20 10:58                         ` Peter C. Chapin
2006-03-20 11:19                           ` Peter C. Chapin
2006-03-20 13:06                           ` Maciej Sobczak
2006-03-20 15:19                         ` Robert A Duff
2006-03-20 16:47                           ` James Dennett
2006-03-20 19:12                         ` Martin Krischik
2006-03-21  7:27                           ` Maciej Sobczak
2006-03-20 19:32                         ` Martin Krischik
2006-03-21  7:41                           ` Maciej Sobczak
2006-03-20 20:29                       ` Simon Wright
2006-03-19 17:43                     ` Larry Kilgallen
2006-03-19 22:11                     ` Peter C. Chapin
2006-03-19 18:15                 ` Robert A Duff
2006-03-19 19:20                   ` Martin Krischik
2006-03-19 20:43                     ` Dr. Adrian Wrigley
2006-03-20 15:01                       ` Robert A Duff
2006-03-27  4:07                       ` Dave Thompson
2006-03-20  9:40                     ` Maciej Sobczak
2006-03-20 15:09                       ` Robert A Duff
2006-03-21  8:07                         ` Maciej Sobczak
2006-03-26 18:53                           ` Robert A Duff
2006-03-19 19:27                 ` Jeffrey R. Carter
2006-03-25 21:40               ` Robert A Duff
2006-03-26  0:10                 ` Justin Gombos
2006-03-26  1:00                   ` Robert A Duff
2006-03-26  6:37                     ` Jeffrey R. Carter
2006-03-26 15:43                       ` Justin Gombos
2006-03-26 16:32                         ` Robert A Duff
2006-03-26 16:51                       ` Robert A Duff
2006-03-26 19:41                         ` Jeffrey R. Carter
2006-03-26  3:15                 ` Frank J. Lhota
2006-03-26 18:28                   ` Robert A Duff
2006-03-26 19:43                     ` Jeffrey R. Carter
2006-03-26 19:59                     ` Simon Wright
  -- strict thread matches above, loose matches on Subject: below --
1999-01-25  0:00 Uninitialized Variables Roger Racine
1999-01-25  0:00 ` Tucker Taft
1999-01-25  0:00 ` robert_dewar
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox