comp.lang.ada
 help / color / mirror / Atom feed
From: Duncan Sands <duncan.sands@math.u-psud.fr>
To: "Robert I. Eachus" <rieachus@comcast.net>, comp.lang.ada@ada-france.org
Subject: Re: C array to Ada pointer to unconstrained array without copying memory
Date: Wed, 8 Oct 2003 11:27:46 +0200
Date: 2003-10-08T11:27:46+02:00	[thread overview]
Message-ID: <mailman.50.1065605164.25614.comp.lang.ada@ada-france.org> (raw)
In-Reply-To: <3F837303.9040202@comcast.net>

On Wednesday 08 October 2003 04:14, Robert I. Eachus wrote:
> Duncan Sands wrote:
> > So far so good.  Now suppose I do:
> >
> > Y : Array_Pointer := new Array_Type' (X);  -- (*)
> >
> > The object I want is Y.  What is the problem?  The problem
> > is that line (*) involves allocating memory and copying the
> > data from X to that memory.  I would like to end up with Y
> > without performing the copy (and without reallocating
> > memory for the array data).
>
> What is wrong with:
>
> Y: Array_Type;
> for Y'Address use X'Address;
>
> ..if you want X and Y to be two different views of the same memory.
> Notice that there is no need for an explicit pointer here, but if X is a
>   pointer as in your example, you may want to do:
>
> Y: Array_Type;
> for Y'Address use X.all'Address;
>
> Oh, and if there is initialization for the elements of Array_Type you
> may want to add a pragma Import; to turn off initialization.

Hi Robert, maybe I can explain it like this.  The data I have is on
the heap.  I want the array bounds on the heap as well.  In the
solution you suggest, the bounds of the variable Y will in general
be allocated on the stack (Y is declared in a local block), not on
the heap.  A fat pointer pointing to the array will have one part
pointing to the data (which is on the heap and obtained from C)
and the other part pointing to the dope (= bounds, and on the
stack).  At least this is how GNAT does it (which is all I care
about).  Once you leave the local block, the fat pointer can no
longer be used because the stack has been wound up and the
bounds are gone.  However, if the bounds were on the heap too,
it would be a different story.

Ciao,

Duncan.



  reply	other threads:[~2003-10-08  9:27 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
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 [this message]
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