comp.lang.ada
 help / color / mirror / Atom feed
* How to pass access parameters to/from C safely/portably?
@ 1998-06-01  0:00 Rod Chapman
  1998-06-05  0:00 ` Rod Chapman
  0 siblings, 1 reply; 3+ messages in thread
From: Rod Chapman @ 1998-06-01  0:00 UTC (permalink / raw)



I wonder if anyone out there could advise me on a point of
portability and style with respect to passing
access parameters from C to Ada.

I need to call Ada subprograms from C, passing C pointers which
denote "in" or "out" parameter for Ada.

Assuming I want C to pass Ada a pointer-to-int...is it
better to do:

   procedure XXX ( Ptr : access Interfaces.C.Int );
   pragma Export(C, XXX);

or

   type Ptr_To_Int is access Interfaces.C.Int;

   procedure XXX ( Ptr : in Ptr_To_Int );
   pragma Export(C, XXX);

I read in Barnes' "Programming in Ada 95" that "In order for this check
to be
possible all access parameters carry with them an indication of the
accessibility
of the actual parameter"...this seems to imply that the first option
above
shouldn't work, since C obviously has no idea of how to pass
accessibility
information...is that right?

Oddly, (using ObjectAda 7.1.1), we seem to have made the first scheme
work, but this might be purely luck!

Thanks in advance,
 Rod Chapman
 Praxis Critical Systems
 rod@praxis-cs.co.uk







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

* Re: How to pass access parameters to/from C safely/portably?
  1998-06-01  0:00 How to pass access parameters to/from C safely/portably? Rod Chapman
@ 1998-06-05  0:00 ` Rod Chapman
  1998-06-06  0:00   ` Robert Dewar
  0 siblings, 1 reply; 3+ messages in thread
From: Rod Chapman @ 1998-06-05  0:00 UTC (permalink / raw)



Rod Chapman wrote:

> I wonder if anyone out there could advise me on a point of
> portability and style with respect to passing
> access parameters from C to Ada.
>

I can close this thread before anyone has even responded!

The answers are:
1) Read the implementation advice in LRM B.3 carefully, and check that
your compiler follows it.

2) Check that your compiler does or doesn't expect accessibility
information to be passed to formal anonymous access parameters
that appear in subprograms which  have pragma Import or Export.

For example, with ObjectAda, I find that applying pragma Export
forces an anonymous access parameter to not have accessibility
checks applied, and the actual parameter can be a simple C
pointer-to-something - this seems to be the obvious implementation,
but it's worth checking...anyone confirm if GNAT does the same?
 - Rod






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

* Re: How to pass access parameters to/from C safely/portably?
  1998-06-05  0:00 ` Rod Chapman
@ 1998-06-06  0:00   ` Robert Dewar
  0 siblings, 0 replies; 3+ messages in thread
From: Robert Dewar @ 1998-06-06  0:00 UTC (permalink / raw)



<<For example, with ObjectAda, I find that applying pragma Export
forces an anonymous access parameter to not have accessibility
checks applied, and the actual parameter can be a simple C
pointer-to-something - this seems to be the obvious implementation,
but it's worth checking...anyone confirm if GNAT does the same?
>>

Yes, of course it does. However, note that access parameters are NOT
an appropriate mapping for pointer types in C calls in general, since
Ada does not permit a null value to be passed as an access parameter.
It may work to suppress access checking (for the call, or for the
procedure), but this is getting quite non-portable. The only "correct"
Ada way of representing a pointer parameter in a C routine is as
a (named) access type on the Ada side. Yes, this is a nuisance, it
may even be considered to be a real oversight in the language design.
It has caused endless trouble in bindings!





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

end of thread, other threads:[~1998-06-06  0:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-06-01  0:00 How to pass access parameters to/from C safely/portably? Rod Chapman
1998-06-05  0:00 ` Rod Chapman
1998-06-06  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