comp.lang.ada
 help / color / mirror / Atom feed
From: Duncan Sands <baldrick@free.fr>
To: comp.lang.ada@ada-france.org
Subject: C array to Ada pointer to unconstrained array without copying memory
Date: Tue, 7 Oct 2003 16:43:00 +0200
Date: 2003-10-07T16:43:00+02:00	[thread overview]
Message-ID: <mailman.37.1065537708.25614.comp.lang.ada@ada-france.org> (raw)

Greetings.  Given a C pointer Ptr to a type T (which points to a block of memory
containing a C array of T's) and the length of the array Length, there are various
tricks for getting an Ada array of the right size (defined on the stack - important!)
that uses the block of memory to hold the array data, i.e. no copying required.

For example,

type Ada_Array is array (Positive range <>) of T;

...

[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!

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,

type Ada_Array_Pointer is access Ada_Array;

...

[have Length and Ptr]

X_Ptr : Ada_Array_Pointer;

[do some clever stuff to set up X_Ptr to point to an array where the
data is given by Ptr, and the bounds are somewhere else, but where? :)]

[Pass X_Ptr around and dereference it etc.]

The problem is the bounds of course.  For example, GNAT usually uses
fat pointers, consisting of two normal pointers where one points to the
data, and the other to the bounds.  So the "clever stuff" will need to
allocate some memory to hold the bounds and set up the fat pointer
appropriately.

Does anyone know a good way to do this?  The solution only needs to
work with GNAT.  I appreciate that deallocating the pointer may need
to be handled specially.

All the best,

Duncan.



             reply	other threads:[~2003-10-07 14:43 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-10-07 14:43 Duncan Sands [this message]
2003-10-07 18:30 ` C array to Ada pointer to unconstrained array without copying memory 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
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