From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,ec21c3c7cdc7ff3e X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local01.nntp.dca.giganews.com!nntp.comcast.com!news.comcast.com.POSTED!not-for-mail NNTP-Posting-Date: Sat, 18 Mar 2006 08:48:23 -0600 Date: Sat, 18 Mar 2006 09:36:24 -0500 From: Jeffrey Creem User-Agent: Mozilla Thunderbird 1.0.7 (Windows/20050923) X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Uninitialized variables References: <1142279908.327131.230200@j52g2000cwj.googlegroups.com> <441C13DB.91C219FE@fakeaddress.nil> In-Reply-To: <441C13DB.91C219FE@fakeaddress.nil> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: NNTP-Posting-Host: 24.147.74.171 X-Trace: sv3-wrAMqsR9GwnXXaj2mr7e0Ezr65xuBg5eeTaQcWEuxWlDlBG2jimtj2lkW9qieWK7oQA8dGPBGTVPSv7!xAlaCokn+bYL0WB5M7FXj5gQtBuqfvKYN7VxdWoDNTaKzgsblvWqS438d08l/aozo2Kxc6uJdANx!+IE= X-Complaints-To: abuse@comcast.net X-DMCA-Complaints-To: dmca@comcast.net X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.32 Xref: g2news1.google.com comp.lang.ada:3434 Date: 2006-03-18T09:36:24-05:00 List-Id: 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".