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!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local01.nntp.dca.giganews.com!nntp.megapath.net!news.megapath.net.POSTED!not-for-mail NNTP-Posting-Date: Mon, 20 Mar 2006 18:08:21 -0600 From: "Randy Brukardt" Newsgroups: comp.lang.ada References: <1142279908.327131.230200@j52g2000cwj.googlegroups.com> Subject: Re: private types Date: Mon, 20 Mar 2006 18:08:25 -0600 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1106 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 Message-ID: NNTP-Posting-Host: 64.32.209.38 X-Trace: sv3-AgtyB/LNyFLH+FnqzajPc9mbd2mUd5ydUqkkmPu2P1dP8pB52ifIqcPBCr6Bp2zL3sto93ZJF0io9Ph!KEfFMYq4aRSyvHolA9Us1JgJxcVj08Dl3IjmewBamXmDm8qH/dT1uZN3R8koH4vJLP5DFFdAXugw X-Complaints-To: abuse@megapath.net X-DMCA-Complaints-To: abuse@megapath.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:3510 Date: 2006-03-20T18:08:25-06:00 List-Id: "Justin Gombos" wrote in message news:I4KSf.552$bu.180@trnddc04... > On 2006-03-18, Randy Brukardt wrote: > > > > Thus, given > > A : Positive := 10; > > B : Positive; > > the compiler can assume that A is in range, potentially being able > > to eliminate checks and speeding up the code. But it cannot assume > > that B is in range (unless it can prove that it is initialized > > further on). > > > > So I recommend initalizing everything (or assigning it immediately > > after the begin) that could be significant to performance. > > As a rule, I try to put readability ahead of optimizations. But if I > did want to write optimum code, I'm not seeing your point here. You're right about premature optimizations, of course. > The runtime checks that might be placed on B need not affect code not > handling B. Assuming an extreme case, suppose B is not assigned until > 100 lines later (ie not immediately following the begin). There > should be no runtime checks in those 100 lines between the 'begin' and > the first assignment to B if B is not referenced (and if B is > referenced prior to assignment, that's a problem that outweighs > excessive checks anyway). The first occurrance of B is going to be an > assignment to B, and it must have the same checks that A would have if > A were being reassigned at this point. So I'm not seeing why more > runtime checks would occur in the case of B. Because, in general, you don't know whether B is initialized. And Ada 95 requires that invalid values be detected before they cause any damage (with some unfortunate exceptions). If B is used to index an array, for instance, it must be checked unless the compiler can prove that it is valid. But that is very hard in general, because of path issues: B : Positive; begin if Bafflegab (10) then B := 10; end if; ... Str (B) ... -- Must check for invalid values here. end; There is no way that the compiler can tell if B has been initialized or not. And Ada 95 does not allow *assuming* that it is initialized (which is essentially what your argument boils down to) -- the compiler must presume the program is incorrect for this purpose unless it can prove that it is not. But note Bob Duff's point that there are other ways to arrange code generators that might have different effects on checking. That's true in general, but in this case in particular, the compiler cannot remove the check for Str (B) no matter what the code generation scheme. If B had been initialized, it would have been able to in most schemes. In any case, in most real code, it's hard to prove something is initialized unless it is done right at the top. Moreover, compilers vary in the amount of flow analysis that they do. So preinitializtion is the way to go for maximum portability. (But I suggest this when you're going to initialize the value anyway, as opposed to initializing it just for this purpose.) Randy.