comp.lang.ada
 help / color / mirror / Atom feed
* Are unconstrained arrays passed by references?
@ 1999-08-10  0:00 bourguet
  1999-08-10  0:00 ` Robert A Duff
  0 siblings, 1 reply; 9+ messages in thread
From: bourguet @ 1999-08-10  0:00 UTC (permalink / raw)


In the Intermetric X bindings, there is something like this in
X.Strings (this is from memory, I may mess up the details but
the principle should be clear):

subtype char is Interfaces.C.char;
type charp is access all char;

function Addr (S : String) return charp;

function To_X (A : System.Address) return charp is
   new Unchecked_Conversion...

function Addr (S : String) return charp is
begin
   return To_X (S (S'First)'Address);
end Addr;

There is a warning to use Addr only with variables and constants
because if the argument was an expression the result may be
dangling before it was of any use.

I understand how this work if the string is passed by reference,
and why passing unconstrained array by reference make sense,
but as I've not found in the RM indications that it is mandatory
I was wondering if I've missed something or if this code rely on
a probable implementation.

Thanks.

-- Jean-Marc


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.




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

* Re: Are unconstrained arrays passed by references?
  1999-08-10  0:00 Are unconstrained arrays passed by references? bourguet
@ 1999-08-10  0:00 ` Robert A Duff
  1999-08-10  0:00   ` bourguet
  0 siblings, 1 reply; 9+ messages in thread
From: Robert A Duff @ 1999-08-10  0:00 UTC (permalink / raw)


bourguet@my-deja.com writes:

> In the Intermetric X bindings, there is something like this in
> X.Strings (this is from memory, I may mess up the details but
> the principle should be clear):
> 
> subtype char is Interfaces.C.char;
> type charp is access all char;
> 
> function Addr (S : String) return charp;
> 
> function To_X (A : System.Address) return charp is
>    new Unchecked_Conversion...
> 
> function Addr (S : String) return charp is
> begin
>    return To_X (S (S'First)'Address);
> end Addr;
> 
> There is a warning to use Addr only with variables and constants
> because if the argument was an expression the result may be
> dangling before it was of any use.
> 
> I understand how this work if the string is passed by reference,
> and why passing unconstrained array by reference make sense,
> but as I've not found in the RM indications that it is mandatory
> I was wondering if I've missed something or if this code rely on
> a probable implementation.

This code is relying on a probable implementation.

Note: on a machine that doesn't have 8-bit storage units (let's say it's
32-bit-word-addressable), and String is packed, then a slice of a string
will normally be passed by copy, if it doesn't happen to be on a word
boundary.  The copy will normally be done at the call site, and the
address of the copy passed.  Of course C doesn't *have* packing, so ...

- Bob
-- 
Change robert to bob to get my real email address.  Sorry.




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

* Re: Are unconstrained arrays passed by references?
  1999-08-10  0:00 ` Robert A Duff
@ 1999-08-10  0:00   ` bourguet
  1999-08-10  0:00     ` Tucker Taft
                       ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: bourguet @ 1999-08-10  0:00 UTC (permalink / raw)


In article <wcc907jhl4c.fsf@world.std.com>,
  Robert A Duff <bobduff@world.std.com> wrote:
> bourguet@my-deja.com writes:
[...]
> > function Addr (S : String) return charp is
> > begin
> >    return To_X (S (S'First)'Address);
> > end Addr;
> >
> > There is a warning to use Addr only with variables and constants
> > because if the argument was an expression the result may be
> > dangling before it was of any use.
> >
> > I understand how this work if the string is passed by reference,
> > and why passing unconstrained array by reference make sense,
> > but as I've not found in the RM indications that it is mandatory
> > I was wondering if I've missed something or if this code rely on
> > a probable implementation.
>
> This code is relying on a probable implementation.

That's what I though, but I wanted to make sure.

Is there a way to guarantee a pass by reference for a given type?
(I seem to recall that in the spitbol package ACT use pragma
volatile to this effect) And for a subtype? (So one can use this
way to create a subtype of string, making this function savier)

> Note: on a machine that doesn't have 8-bit storage units (let's say
> it's 32-bit-word-addressable), and String is packed, then a slice of
> a string will normally be passed by copy, if it doesn't happen to be
> on a word boundary.  The copy will normally be done at the call site,
> and the address of the copy passed.

That's a good raison to use the type char_array which is provided by
the bindings (perhaps as a subtype of a type in Interfaces.C or
I.C.Strings) to declare constants.

BTW, I'd not have passed a slice, due to the warning of not using
expression.

> Of course C doesn't *have* packing, so ...

I though that one raison for which several types in C may have
different pointer size what to make it possible to use packed arrays
of char. But I may be wrong.

-- Jean-Marc


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.




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

* Re: Are unconstrained arrays passed by references?
  1999-08-10  0:00   ` bourguet
  1999-08-10  0:00     ` Tucker Taft
@ 1999-08-10  0:00     ` Ted Dennison
  1999-08-11  0:00     ` Robert A Duff
  2 siblings, 0 replies; 9+ messages in thread
From: Ted Dennison @ 1999-08-10  0:00 UTC (permalink / raw)


In article <7opbch$bca$1@nnrp1.deja.com>,
  bourguet@my-deja.com wrote:
> Is there a way to guarantee a pass by reference for a given type?
> (I seem to recall that in the spitbol package ACT use pragma
> volatile to this effect) And for a subtype? (So one can use this
> way to create a subtype of string, making this function savier)

Well, you could pass a pointer to the object.

In Ada83 that was the only portable way. In Ada(95) there is now the
"access" parameter mode. Also, the LRM now defines several situations in
which a specific mechanism *must* be used, depending on the type of the
object being passed.

--
T.E.D.


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.




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

* Re: Are unconstrained arrays passed by references?
  1999-08-10  0:00   ` bourguet
@ 1999-08-10  0:00     ` Tucker Taft
  1999-08-10  0:00       ` Robert Dewar
  1999-08-10  0:00     ` Ted Dennison
  1999-08-11  0:00     ` Robert A Duff
  2 siblings, 1 reply; 9+ messages in thread
From: Tucker Taft @ 1999-08-10  0:00 UTC (permalink / raw)


bourguet@my-deja.com wrote:
> 
> In article <wcc907jhl4c.fsf@world.std.com>,
>   Robert A Duff <bobduff@world.std.com> wrote:
> > bourguet@my-deja.com writes:
> [...]
> > > function Addr (S : String) return charp is
> > > begin
> > >    return To_X (S (S'First)'Address);
> > > end Addr;
> > >
> > > There is a warning to use Addr only with variables and constants
> > > because if the argument was an expression the result may be
> > > dangling before it was of any use.
> > >
> > > I understand how this work if the string is passed by reference,
> > > and why passing unconstrained array by reference make sense,
> > > but as I've not found in the RM indications that it is mandatory
> > > I was wondering if I've missed something or if this code rely on
> > > a probable implementation.
> >
> > This code is relying on a probable implementation.
> 
> That's what I though, but I wanted to make sure.
> 
> Is there a way to guarantee a pass by reference for a given type?

If the subprogram has convention "C" then you can be sure that
arrays are passed by reference.

> 
> -- Jean-Marc

-- 
-Tucker Taft   stt@averstar.com   http://www.averstar.com/~stt/
Technical Director, Distributed IT Solutions  (www.averstar.com/tools)
AverStar (formerly Intermetrics, Inc.)   Burlington, MA  USA




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

* Re: Are unconstrained arrays passed by references?
  1999-08-10  0:00     ` Tucker Taft
@ 1999-08-10  0:00       ` Robert Dewar
  1999-08-11  0:00         ` Larry Kilgallen
  0 siblings, 1 reply; 9+ messages in thread
From: Robert Dewar @ 1999-08-10  0:00 UTC (permalink / raw)


In article <37B071C8.FF759493@averstar.com>,
  Tucker Taft <stt@averstar.com> wrote:
> If the subprogram has convention "C" then you can be sure that
> arrays are passed by reference.

I disagree, this is implementation advice only, not a
requirement of the standard. For example, it would be perfectly
valid to pass a 32 bit packed array as an int (it would not
meet the implementation advice, but it would be valid).


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.




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

* Re: Are unconstrained arrays passed by references?
  1999-08-10  0:00       ` Robert Dewar
@ 1999-08-11  0:00         ` Larry Kilgallen
  0 siblings, 0 replies; 9+ messages in thread
From: Larry Kilgallen @ 1999-08-11  0:00 UTC (permalink / raw)


In article <7oq77c$1et$1@nnrp1.deja.com>, Robert Dewar <robert_dewar@my-deja.com> writes:
> In article <37B071C8.FF759493@averstar.com>,
>   Tucker Taft <stt@averstar.com> wrote:
>> If the subprogram has convention "C" then you can be sure that
>> arrays are passed by reference.
> 
> I disagree, this is implementation advice only, not a
> requirement of the standard. For example, it would be perfectly
> valid to pass a 32 bit packed array as an int (it would not
> meet the implementation advice, but it would be valid).

Doesn't this depend on the C compiler to which you are
interfacing ?  I know VAX C can accept an array of 9
bytes passed by value (it spreads the bytes across
the registers when running on Alpha).  I presume DEC
C can do the same thing for the sake of compatibility.

The only things I know about C compilers are the ugly things.

Larry Kilgallen




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

* Re: Are unconstrained arrays passed by references?
  1999-08-10  0:00   ` bourguet
  1999-08-10  0:00     ` Tucker Taft
  1999-08-10  0:00     ` Ted Dennison
@ 1999-08-11  0:00     ` Robert A Duff
  1999-08-11  0:00       ` Robert Dewar
  2 siblings, 1 reply; 9+ messages in thread
From: Robert A Duff @ 1999-08-11  0:00 UTC (permalink / raw)


bourguet@my-deja.com writes:

> Is there a way to guarantee a pass by reference for a given type?

There are several ways -- look up "by-reference" in the RM index.
Eg a limited record is always passed by reference.

> BTW, I'd not have passed a slice, due to the warning of not using
> expression.

A slice is a name, and a slice of an object is an object.

> I though that one raison for which several types in C may have
> different pointer size what to make it possible to use packed arrays
> of char. But I may be wrong.

Well, maybe, but the C compilers I know of on word-addressable machines
don't pack.  It's not clear how one would *ask* for packing in C,
and it's not clear that it should be turned on by default (if at
all).

- Bob
-- 
Change robert to bob to get my real email address.  Sorry.




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

* Re: Are unconstrained arrays passed by references?
  1999-08-11  0:00     ` Robert A Duff
@ 1999-08-11  0:00       ` Robert Dewar
  0 siblings, 0 replies; 9+ messages in thread
From: Robert Dewar @ 1999-08-11  0:00 UTC (permalink / raw)


In article <wccr9laijl8.fsf@world.std.com>,
  Robert A Duff <bobduff@world.std.com> wrote:
> Well, maybe, but the C compilers I know of on word-addressable
machines
> don't pack.  It's not clear how one would *ask* for packing in
C,
> and it's not clear that it should be turned on by default (if
at
> all).


Surely the C compiler for the Cray-2 does pack strings, and
uses specialized pointers for characters? At least that was
my impression, and the C language definition is clearly
carefully designed to permit this implementation approach.


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.




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

end of thread, other threads:[~1999-08-11  0:00 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-08-10  0:00 Are unconstrained arrays passed by references? bourguet
1999-08-10  0:00 ` Robert A Duff
1999-08-10  0:00   ` bourguet
1999-08-10  0:00     ` Tucker Taft
1999-08-10  0:00       ` Robert Dewar
1999-08-11  0:00         ` Larry Kilgallen
1999-08-10  0:00     ` Ted Dennison
1999-08-11  0:00     ` Robert A Duff
1999-08-11  0:00       ` Robert Dewar

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