comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: Concatenation and Characters
Date: Tue, 22 Oct 2002 21:31:58 GMT
Date: 2002-10-22T21:31:58+00:00	[thread overview]
Message-ID: <wccd6q29n3l.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: urap57ajqc4rc5@corp.supernews.com

Jeffrey Carter <jrcarter@acm.org> writes:

> Matthew Heaney wrote:
> > This isn't quite right.  If an object of a discrete type is used to index an
> > array, the compiler is required to ensure that the object --even if
> > uninitialized-- is only used to index an actual component of the array
> > object.
> > For example:
> > procedure Op (S : String) is
> >   I : Positive;
> > begin
> >   S (I) := 'x';
> > end;
> 
> I presume you mean "S : in out String"?
> 
> > The Ada95 language guarantees that index I will only touch the memory
> > owned
> > by array object S.
> > This is one area where Ada95 differs from Ada83, which made no such
> > guarantee.
> 
> This has nothing to do with detecting a reference to an uninitialized
> variable.

It has to do with uninitialized variables, certainly, if not their
"detection".  In Ada 83, there was no requirement for an array bounds
check in the above program.  That's because the execution was erroneous.
In Ada 95, there *is* a requirement for an array bounds check.
In fact, the *only* purpose of a bounds check in the above example is to
detect uninit vars -- if I were initialized, there would be no need for
the check.  So I think your "...nothing to do with..." claim is
overstated at best.

This *does* make a difference in practise.  Ada 83 compilers *were*
sometimes smart enough to notice that "S(I) := ..." does not need a
range check, and eliminated the check, so that statement could destroy
arbitrary memory locations.  Ada 95 compilers are not allowed to do that
optimization, unless they can prove I is initialized.

The Ada 83 optimizer could reason as follows:

    I is of subtype Positive.
    So either the value of I is in Positive, or else I is uninitialized.
    If I is in Positive, we can leave out the array bounds check,
    because the index subtype of String is also Positive.
    If I is uninitialized, we can leave out the check because
    the program execution will be erroneous (unpredictable).

This reasoning is incorrect for an Ada 95 compiler, and the difference
is precisely in the semantics of uninit vars.

>...It is about bound checking of array indexing, which did exist
> in Ada 83.

No, as I said, bounds checking did *not* exist in Ada 83 for the above
example.  (Yes, bounds checking did exist -- but not in the cases we're
talking about.)

>... Unless run-time checks are suppressed, this behaves as if it
> were written
> 
> procedure Op (S : in out String) is
>     I : Positive;
> begin
>     if I not in S'range then
>        raise Constraint_Error;
>     end if;
> 
>     S (I) := 'x';
> end Op;
> 
> This was true in Ada 83.

Yes, this is essentially equivalent.  In Ada 83, the compiler was
allowed to optimize away the entire 'if' statement.  In Ada 95, it is
not (unless it somehow knows that I is initialized).

>... This is true even if I is initialized:
> 
> -- In Op:
> I : Positive := 7;
> 
> -- Elsewhere:
> X : String := "abcdefg";
> ...
> Op (S => X (3 .. 5) ); -- Raises Constraint_Error

Correct.

> With run-time checks suppressed, what happens is anyone's guess, in Ada
> 83 and Ada.

Correct.

- Bob



  parent reply	other threads:[~2002-10-22 21:31 UTC|newest]

Thread overview: 72+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-10-10 14:50 Concatenation and Characters Justin Birtwell
2002-10-10 14:55 ` Preben Randhol
2002-10-10 15:04   ` Justin Birtwell
2002-10-10 15:22     ` Preben Randhol
2002-10-10 15:30       ` Justin Birtwell
2002-10-10 16:05         ` Georg Bauhaus
2002-10-10 16:07         ` Preben Randhol
2002-10-10 17:45         ` Robert A Duff
2002-10-10 15:32       ` Justin Birtwell
2002-10-10 15:36         ` Preben Randhol
2002-10-10 16:44         ` Mark Biggar
2002-10-10 17:45           ` Stephen Leake
2002-10-10 21:53             ` Mark Biggar
2002-10-18 17:03           ` Programmer Dude
2002-10-18 18:13             ` Preben Randhol
2002-10-18 18:36             ` Wes Groleau
2002-10-21 15:16               ` Georg Bauhaus
2002-10-18 21:33             ` Mark Biggar
2002-10-20  2:01               ` Dmitry A.Kazakov
2002-10-21 14:13                 ` Wes Groleau
2002-10-21 15:22                   ` Dmitry A. Kazakov
2002-10-21 19:38                     ` Georg Bauhaus
2002-10-22 22:15                       ` Dmitry A.Kazakov
2002-10-22 12:05                         ` Georg Bauhaus
2002-10-22 12:19                           ` Lutz Donnerhacke
2002-10-22 14:43                             ` Georg Bauhaus
2002-10-23  8:39                           ` Dmitry A. Kazakov
2002-10-23 14:39                             ` Georg Bauhaus
2002-10-24  8:18                               ` Dmitry A. Kazakov
2002-10-21 16:50                   ` Warren W. Gay VE3WWG
2002-10-21 15:20             ` Georg Bauhaus
2002-10-21 17:51               ` Programmer Dude
2002-10-21 18:48                 ` Jim Rogers
2002-10-21 19:44                   ` tmoran
2002-10-21 20:42                   ` Programmer Dude
2002-10-22  1:42                     ` Jeffrey Carter
2002-10-22 14:37                       ` Robert A Duff
2002-10-22 18:51                         ` Jeffrey Carter
2002-10-23  7:01                         ` Pascal Obry
2002-10-22 14:45                       ` Matthew Heaney
2002-10-22 18:47                         ` Jeffrey Carter
2002-10-22 21:31                         ` Robert A Duff [this message]
     [not found]                         ` <3DB59D75.20609 <wccd6q29n3l.fsf@shell01.TheWorld.com>
2002-10-23  2:02                           ` Jeffrey Carter
2002-10-23 13:16                             ` Matthew Heaney
2002-10-23 19:11                               ` Jeffrey Carter
2002-10-23 15:24                             ` Robert A Duff
2002-10-23 19:24                               ` Jeffrey Carter
2002-10-24  0:33                                 ` Robert A Duff
2002-10-22  3:46                     ` Jim Rogers
2002-10-22 14:48                       ` Robert A Duff
2002-10-22 15:02                         ` Fraser Wilson
2002-10-22 15:38                           ` David C. Hoos
2002-10-22 15:44                             ` Fraser Wilson
2002-10-22 16:13                         ` Robert A Duff
2002-10-23  8:58                           ` Dmitry A. Kazakov
2002-10-23  9:08                             ` Lutz Donnerhacke
2002-10-23  9:34                               ` Dmitry A. Kazakov
2002-10-23 10:10                                 ` Lutz Donnerhacke
2002-10-23 17:15                                 ` Frank J. Lhota
2002-10-24  8:41                                   ` Dmitry A. Kazakov
2002-10-24  9:25                                   ` Fraser Wilson
2002-10-24 14:13                                     ` Matthew Heaney
     [not found]                         ` <un <wcc7kgazc20.fsf@shell01.TheWorld.com>
2002-10-22 16:46                           ` David C. Hoos
2002-10-22  8:51                   ` Stuart Palin
2002-10-22 18:56                     ` Programmer Dude
2002-10-21 19:42                 ` Georg Bauhaus
  -- strict thread matches above, loose matches on Subject: below --
2002-10-11  5:04 Grein, Christoph
2002-10-11 10:30 ` Preben Randhol
2002-10-23  5:15 Grein, Christoph
2002-10-23 13:19 ` Matthew Heaney
2002-10-24  5:53 Grein, Christoph
2002-10-24 14:04 ` Matthew Heaney
replies disabled

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