comp.lang.ada
 help / color / mirror / Atom feed
From: "Matthew Heaney" <matthew_heaney@acm.org>
Subject: Re: Interfacing to C non-void procedure with "out" parameters
Date: 2000/01/17
Date: 2000-01-17T00:00:00+00:00	[thread overview]
Message-ID: <jTHg4.2072$4R4.102692@newsread1.prod.itd.earthlink.net> (raw)
In-Reply-To: 387f1c30.12554815@news.rdg.ac.uk

In article <387f1c30.12554815@news.rdg.ac.uk> , 
S.J.Sangwine@Reading.ac.uk (Dr Steve Sangwine) wrote:

> Can anyone shed light on the issue of interfacing between Ada95 and C
> where there is a C procedure (non-void) which returns a result *and*
> modifies some of its parameters?

In neither Ada nor C can you directly modify a function parameter.  In
both languages you have to pass a pointer to the data, and modify the
data designated by the pointer.

Given this C header:

int op (float *o);

You can bind to it from Ada like this

with Interfaces.C;  use Interfaces.C;
package P is

  type Float_Access is access all C_Float;
  pragma Convention (C, Float_Access);

  function Op (O : Float_Access) return Int;

  pragma Import (C, Op, "op");

end P;



> The LRM, Appendix B doesn't say anything about this, and neither does
> the Ada95 Rationale as far as I can see.

There's nothing magic about the fact that the C procedure has a non-void
return value.  Just import the C procedure as an Ada function.


> I have a C procedure, for which I wanted to write an Ada95 spec, and
> import the C object code using pragma Import. Since the C procedure
> has a return result, I need an Ada function (or do I?).

Yes.  Unless you want to throw away the procedure's return value, in
which case you can bind to it using an Ada procedure.


> An Ada
> function must have parameters of mode 'in', implicitly or explicity,
> yet the C procedure will modify one or more of its parameters.

It's impossible for C to modify a parameter.

Actually, I should qualify the above statement.  In C, you can modify a
parameter, for use as a local variable.  However, any changes you make
to the value of the parameter remain local, and are not transmitted back
to the caller.

The only way to modify a caller's value is the pass the address of the
object to a C procedure.

> To be
> safe, I wrote a wrapper procedure in C which had an extra parameter,
> and called this from Ada. The wrapper procedure called the real C
> procedure and returned the return result of the real C procedure in
> the extra parameter. That way the Ada spec can be a procedure and
> everything is watertight, but it is a nuisance to have this extra,
> trivial C wrapper procedure in between.

It's a nuisance, and unnecessary.  Get rid of it.


> Would it work to allow the C procedure to modify a parameter that
> the Ada code thinks is read-only (i.e. 'in'), or should I play safe as
> described above. (By 'work', I mean work portably, not work when
> compiled with a specific compiler.)


You are free to modify a parameter on the C side that Ada thinks is in,
just as can with a C caller.  However, these changes are not propagated
back to the Ada client, nor to a C client.


> Since C programmers routinely confuse the concepts of procedure and
> function (rant ....) this issue arises with almost any C code that one
> wants to interface to Ada.

There is no issue.


--
You cannot think without abstractions; accordingly, it is of the utmost
importance to be vigilant in critically revising your modes of
abstraction.  It is here that philosophy finds its niche as essential to
the healthy progress of society.  It is the critic of abstractions.

Alfred North Whitehead




  parent reply	other threads:[~2000-01-17  0:00 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-01-14  0:00 Interfacing to C non-void procedure with "out" parameters Dr Steve Sangwine
2000-01-14  0:00 ` Alain Le Guennec
2000-01-14  0:00 ` Ted Dennison
2000-01-14  0:00   ` Jeff Carter
2000-01-14  0:00     ` Ted Dennison
2000-01-15  0:00       ` Jeff Carter
2000-01-17  0:00 ` Matthew Heaney [this message]
2000-01-18  0:00   ` Dr Steve Sangwine
replies disabled

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