From: Jeffrey Carter <spam@spam.com>
Subject: Re: C array to Ada pointer to unconstrained array without copying memory
Date: Tue, 07 Oct 2003 20:00:09 GMT
Date: 2003-10-07T20:00:09+00:00 [thread overview]
Message-ID: <dXEgb.1511$av5.23@newsread3.news.pas.earthlink.net> (raw)
In-Reply-To: <mailman.37.1065537708.25614.comp.lang.ada@ada-france.org>
Duncan Sands wrote:
> [have Length and Ptr]
>
> subtype Array2 is Ada_Array (1 .. Length);
>
> X : Array2;
> for X'Address use Ptr; -- ok, Ptr should be of type System'Address, but hey!
You should also have
pragma Import (Ada, X);
to avoid initializing X. But this may not provide usable bounds for X.
If, for example, your compiler stores the bounds in memory just before
the 1st element of X, this may result in garbage for the bounds of X.
The general technique is to use Ptr as an access value to something as
large or larger than Length can ever be, then immediately take a slice
and pass it elsewhere. Slicing generates the bounds information. The
following compiles:
procedure Bound_Generation is
type T is new Integer;
type Real is array (Positive range <>) of T;
subtype Huge is Real (Positive);
type Huge_Ptr is access all Huge;
pragma Convention (C, Huge_Ptr);
X_Ptr : Huge_Ptr;
Length : Positive;
procedure Deal_With_X (X : in Real) is
begin -- Deal_With_X
null;
end Deal_With_X;
begin -- Bound_Generation
-- Get X_Ptr and Length from C
Deal_With_X (X => X_Ptr (1 .. Length) );
end Bound_Generation;
> And then you can make use of X. However being defined on the stack, it
> can be quite awkward to use. It would be nice to have the same thing but
> with X dynamically allocated. For example,
There is generally no need for a pointer to X; I'd have to know more
about what you're trying to do to understand why you say you want one.
With GNAT, you could use 'Unrestricted_Access on X within Deal_With_X to
obtain such a pointer. Or, instead of calling a subprogram, you could do
something like:
declare
type Real_Ptr is access all Real;
X : Real_Ptr;
Result : aliased Real := X_Ptr (1 .. Length);
begin
X := Result'Access;
-- Deal with X here
end;
--
Jeff Carter
"Your mother was a hamster and your father smelt of elderberries."
Monty Python & the Holy Grail
06
next prev parent reply other threads:[~2003-10-07 20:00 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-10-07 14:43 C array to Ada pointer to unconstrained array without copying memory Duncan Sands
2003-10-07 18:30 ` tmoran
2003-10-07 19:24 ` Duncan Sands
2003-10-08 0:02 ` sk
[not found] ` <3F83540E.5000302@myob.com>
2003-10-08 9:18 ` Duncan Sands
2003-10-07 20:00 ` Jeffrey Carter [this message]
2003-10-07 20:39 ` Duncan Sands
2003-10-08 1:27 ` Jeffrey Carter
2003-10-07 20:53 ` Chad R. Meiners
2003-10-07 21:24 ` Duncan Sands
2003-10-07 22:44 ` C array to Ada pointer to unconstrained array without copyingmemory Chad R. Meiners
2003-10-07 22:52 ` Chad R. Meiners
2003-10-08 9:20 ` Duncan Sands
2003-10-08 16:16 ` C array to Ada pointer to unconstrained array withoutcopyingmemory Chad R. Meiners
2003-10-08 16:49 ` Duncan Sands
2003-10-08 2:14 ` C array to Ada pointer to unconstrained array without copying memory Robert I. Eachus
2003-10-08 9:27 ` Duncan Sands
2003-10-08 22:43 ` Robert I. Eachus
2003-10-09 9:31 ` Duncan Sands
2003-10-08 14:07 ` Steve Adams
2003-10-08 14:33 ` Preben Randhol
2003-10-09 23:04 ` Steve Adams
2003-10-09 23:11 ` Steve Adams
2003-10-08 16:30 ` Duncan Sands
2003-10-09 22:59 ` C array to Ada pointer to unconstrained array without copyingmemory Steve Adams
2003-10-10 7:02 ` Duncan Sands
2003-10-10 16:44 ` C array to Ada pointer to unconstrained array withoutcopyingmemory Steve Adams
2003-10-23 21:27 ` C array to Ada pointer to unconstrained array without copying memory Craig Carey
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox