comp.lang.ada
 help / color / mirror / Atom feed
From: Duncan Sands <duncan.sands@math.u-psud.fr>
To: Jeffrey Carter <spam@spam.com>, comp.lang.ada@ada-france.org
Subject: Re: C array to Ada pointer to unconstrained array without copying memory
Date: Tue, 7 Oct 2003 22:39:54 +0200
Date: 2003-10-07T22:39:54+02:00	[thread overview]
Message-ID: <mailman.40.1065559101.25614.comp.lang.ada@ada-france.org> (raw)
In-Reply-To: <dXEgb.1511$av5.23@newsread3.news.pas.earthlink.net>

Hi Jeff, thanks for your reply.

> 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;

Right.  With GNAT there is no problem with the method I mentioned, but this method
indeed seems more portable.  A few other methods are discussed on the page
http://www.adapower.com/lang/accessmem.html

> > 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.

I'm a bit surprised you ask.  Your comment is analogous to "there is
generally no need to use pointers to strings".  And indeed, you often
don't need them, but there are plenty of legitimate, not to say essential
uses for them.  I would like to pass a pointer to X all around my program.

> With GNAT, you could use 'Unrestricted_Access on X within Deal_With_X to
> obtain such a pointer. 

Woah!  Not so!  (Or at least so it seems to me).  This pointer to X will be
a fat pointer, with one part pointing the the data (i.e. X_Ptr) - and this can
be used after Deal_With_X returns -, and another part pointing to the bounds.
And where are these bounds stored?  Right, on the stack (or some other
convenient temporary place), and that bit of memory will go away once
Deal_With_X returns.  So if I use Unrestricted_Access to get a pointer
to X and stick it in (for example) a global variable, then accessing X
through it after returning from Deal_With_X will lead to swift punishment.

> 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;

But I would like

-- Deal with X here

(after the end;).

And this seems tricky.

All the best,

Duncan.



  reply	other threads:[~2003-10-07 20:39 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
2003-10-07 20:39   ` Duncan Sands [this message]
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