comp.lang.ada
 help / color / mirror / Atom feed
From: Tucker Taft <stt@avercom.net>
Subject: Re: When to do a constraint check and not ??
Date: Thu, 01 Nov 2001 12:54:14 -0500
Date: 2001-11-01T17:54:09+00:00	[thread overview]
Message-ID: <3BE18C46.1AB713B4@avercom.net> (raw)
In-Reply-To: 3BD91EF8.EEB7EDBA@systems.saab.se

Per Sandberg wrote:
> 
> Assume the folowing code:
> 
> procedue bla is
>         b : boolean; --< B may contain any bit pattern at this point.
> 
>         procedure test( p : in out boolean ) is
>         begin
>                 b := false;

I presume you meant to write: "p := false;" here.

>         end test;
> 
> begin
>         test(b); --<< Is the compiler allowed to insert a constraint check on b
> here ??

Yes, it may perform a constraint check here.  In Ada 95,
referencing an uninitialized variable is a "bounded error"
(whereas in Ada 83 it was "erroneous").  This typically
means that an Ada 95 compiler won't "believe" the subtype
declaration of an object if it hasn't been initialized.
Hence, it will need to check its value against the
bounds of the subtype before it assigns it to an
object that it *does* believe is in range.  Certainly
all compilers will want to believe that "in" and "in out"
parameters are in range, so the (implicit) assignment
from b to the formal parameter p when the function is
called will need to ensure the value is in range.
If it happens to have "stack junk" in it which puts it
outside the typical 0..1 range used for booleans, then
you will get a constraint_error raised here.

So unless a compiler otherwise ensures that "b" is in
range, it will want to perform a constraint check
when it is passed to "test."  As others have
suggested, if you don't want a constraint check
there, either initialize b, or make "p" into an "out"
parameter rather than "in out".

> end;
> 
> Is the answer that it is up to the wendor.
> I did not get any clues from the LRM.

It is up to the vendor in some sense, but the
fact that referencing an uninitialized variable
is only a "bounded error" makes it quite
likely that a check will be performed at this
point to avoid having an uninitialized variable
"poison" all IN and IN OUT parameters.

> 
> /Per Sandberg.

-- 
-Tucker Taft   stt@avercom.net   http://www.avercom.net
Chief Technology Officer, AverCom Corporation (A Titan Company) 
Bedford, MA  USA (AverCom was formerly the Commercial Division of AverStar:
http://www.averstar.com/~stt)



  parent reply	other threads:[~2001-11-01 17:54 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-10-26  8:29 When to do a constraint check and not ?? Per Sandberg
2001-10-26 12:01 ` Peter Hend�n
2001-10-26 13:57 ` DuckE
2001-10-26 14:18 ` Ted Dennison
2001-10-26 23:44 ` Jeffrey Carter
2001-11-01 17:54 ` Tucker Taft [this message]
2001-11-02  3:44   ` Robert Dewar
2001-11-05 10:02 ` Peter Amey
replies disabled

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