From: Simon Wright <simon@pogner.demon.co.uk>
Subject: Re: Interfacing to C, output arrays
Date: 1996/12/03
Date: 1996-12-03T00:00:00+00:00 [thread overview]
Message-ID: <x7vhgm4p1gs.fsf@pogner.demon.co.uk> (raw)
In-Reply-To: dewar.849568380@merv
dewar@merv.cs.nyu.edu (Robert Dewar) writes:
> Simon asks about:
>
> with Interfaces.C;
> package Inter is
> type Arr is array (Integer range <>) of Interfaces.C.Int;
> procedure Accessor (Result : out Arr);
> end Inter;
>
> package body Inter is
> procedure Accessor (Result : out Arr) is
> subtype This_Arr is Arr (Result'Range);
> function Accessor (N : Interfaces.C.Int;
> Result : This_Arr) return Interfaces.C.Int;
> pragma Import (C, Accessor, "accessor");
> Count : Interfaces.C.Int;
> begin
> Count := Accessor (N => Result'Length, Result => Result);
> end Accessor;
> end Inter;
>
> And he surmises that RM B.3(70) makes this safe.
>
> Well, I certainly think that GNAT is right to output a warning here, at
> the least it is very ugly to have a C routine modifying an in parameter,
> and indeed I think a compiler would be within its rights (though perhaps
> not well advised) to assume that the function call could not modify the
> array.
Yes, I quite understand that; the problem is that the C subprogram is
a function and therefore can't be declared with an out parameter. I
guess I could use the DEC pragma (valued_subprogram??), but that would
be a tad implementation-dependent!
> It would be much cleaner to pass an access to the array or to use a
> procedure call and make the parameter out or in out.
The revised version below uses the access idea, at the cost of a copy.
Is this the only proper solution?
> Also note that RM B.3(70) is only implementation advice, not a requirement.
Point taken.
--
with Interfaces.C;
package Inter is
type Arr is array (Integer range <>) of Interfaces.C.Int;
procedure Accessor (Result : out Arr);
end Inter;
package body Inter is
procedure Accessor (Result : out Arr) is
subtype This_Arr is Arr (Result'Range);
function Accessor (N : Interfaces.C.Int;
Result : access This_Arr) return Interfaces.C.Int;
pragma Import (C, Accessor, "accessor");
Count : Interfaces.C.Int;
Res : aliased This_Arr;
begin
Count := Accessor (N => Result'Length, Result => Res'Access);
Result := Res;
end Accessor;
end Inter;
next prev parent reply other threads:[~1996-12-03 0:00 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
1996-12-02 0:00 Interfacing to C, output arrays Simon Wright
1996-12-02 0:00 ` Robert Dewar
1996-12-03 0:00 ` Simon Wright [this message]
1996-12-07 0:00 ` Robert Dewar
1996-12-10 0:00 ` Paul Chardon
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox