comp.lang.ada
 help / color / mirror / Atom feed
* What is X'Address of an empty array?
@ 2022-05-31 12:19 Dmitry A. Kazakov
  2022-05-31 21:35 ` Randy Brukardt
  0 siblings, 1 reply; 3+ messages in thread
From: Dmitry A. Kazakov @ 2022-05-31 12:19 UTC (permalink / raw)


I have a language lawyering question. According to ARM X'Address is the 
address of the first array element. What is the address of empty array?

In the case of an array with bounds it could be the address following 
the bounds.

But what about a definite empty array? Of zero length (and presumably 
zero size). Would the compiler have to invent some address?

P.S. With GNAT:

    type NUL is array (1..0) of Integer;
    S : NUL;

S'Size is 8 and it has some address that holds the byte.

Talking about the dark matter in our Universe. This is what empty arrays 
are constructed of! (:-))

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: What is X'Address of an empty array?
  2022-05-31 12:19 What is X'Address of an empty array? Dmitry A. Kazakov
@ 2022-05-31 21:35 ` Randy Brukardt
  2022-06-01 12:49   ` Dmitry A. Kazakov
  0 siblings, 1 reply; 3+ messages in thread
From: Randy Brukardt @ 2022-05-31 21:35 UTC (permalink / raw)


Aliased objects should never have zero-size in Ada (or, at least, ever be 
allocated at the same address). I believe that is because of the required 
result of the equality operator. Specifically:

    type NUL is array (1..0) of Integer;
    A, B : aliased NUL;
    type PNul is access Nul;
    PA : PNul := A'Access; PB : PNul := B'Access;

    if PA = PB then
         Report.Failed ("Access to two distinct objects can not be equal");
    end if;

If an object is not aliased, it is undefined whether 'Address will work 
reliably with it (it probably does in GNAT, it might not in Janus/Ada, etc.) 
If the objects ARE aliased, then 'Address works essentially the same as 
'Access.

I personally find this a bit of overspecification in Ada, but since 
zero-size objects are unusual, no one has thought it worth going through the 
effort to change. (And of course such a change would complicate static 
analysis.) We (the ARG) did discuss this topic at one point (I don't have 
the AI number at hand).

                                   Randy.




"Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> wrote in message 
news:t7514c$11tj$1@gioia.aioe.org...
>I have a language lawyering question. According to ARM X'Address is the 
>address of the first array element. What is the address of empty array?
>
> In the case of an array with bounds it could be the address following the 
> bounds.
>
> But what about a definite empty array? Of zero length (and presumably zero 
> size). Would the compiler have to invent some address?
>
> P.S. With GNAT:
>
>    type NUL is array (1..0) of Integer;
>    S : NUL;
>
> S'Size is 8 and it has some address that holds the byte.
>
> Talking about the dark matter in our Universe. This is what empty arrays 
> are constructed of! (:-))
>
> -- 
> Regards,
> Dmitry A. Kazakov
> http://www.dmitry-kazakov.de 


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: What is X'Address of an empty array?
  2022-05-31 21:35 ` Randy Brukardt
@ 2022-06-01 12:49   ` Dmitry A. Kazakov
  0 siblings, 0 replies; 3+ messages in thread
From: Dmitry A. Kazakov @ 2022-06-01 12:49 UTC (permalink / raw)


On 2022-05-31 23:35, Randy Brukardt wrote:
> Aliased objects should never have zero-size in Ada (or, at least, ever be
> allocated at the same address). I believe that is because of the required
> result of the equality operator. Specifically:
> 
>      type NUL is array (1..0) of Integer;
>      A, B : aliased NUL;
>      type PNul is access Nul;
>      PA : PNul := A'Access; PB : PNul := B'Access;
> 
>      if PA = PB then
>           Report.Failed ("Access to two distinct objects can not be equal");
>      end if;
> 
> If an object is not aliased, it is undefined whether 'Address will work
> reliably with it (it probably does in GNAT, it might not in Janus/Ada, etc.)
> If the objects ARE aliased, then 'Address works essentially the same as
> 'Access.
> 
> I personally find this a bit of overspecification in Ada, but since
> zero-size objects are unusual, no one has thought it worth going through the
> effort to change. (And of course such a change would complicate static
> analysis.) We (the ARG) did discuss this topic at one point (I don't have
> the AI number at hand).

It is actually rather nice.

I recently stumbled upon code:

    A (A'First)'Address

in C bindings, when the other side has something like:

    const double * a, int len

It of course fails in the marginal case when the array is empty. But 
A'Address seems never do.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-06-01 12:49 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-31 12:19 What is X'Address of an empty array? Dmitry A. Kazakov
2022-05-31 21:35 ` Randy Brukardt
2022-06-01 12:49   ` Dmitry A. Kazakov

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