comp.lang.ada
 help / color / mirror / Atom feed
From: Duncan Sands <baldrick@free.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: Thu, 9 Oct 2003 11:31:06 +0200
Date: 2003-10-09T11:31:06+02:00	[thread overview]
Message-ID: <mailman.55.1065691794.25614.comp.lang.ada@ada-france.org> (raw)
In-Reply-To: <3F849301.8090804@comcast.net>

> > 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.
>
> Then put them there!  I'll leave the details to you, since you know what
> you are trying to do.  But I'll explain what you seem to want.
>
> You have an object, in C, that you "know" the bounds of.  You want to
> write a procedure that can accept a (C) pointer to the data on the heap,
> and construct an Ada object that you can work with.  You also don't seem
> to want to move the data.  (If you are willing to do so, this all gets a
> lot easier.)
>
> Now define an Ada record type, probably in a (library) package spec,
> since you want to be able to pass objects of the type around.  The
> package will be implementing what is referred to as an abstract data
> type.  You can make the type private and put the implementation details
> in the private part of the spec, or even in the body if you really want
> to.  But easiest is to let other units see the implementation at first.

Hi Robert, thanks for your help.  Don't worry - I know all about abstract
data types.  I've been using Ada for many years now :)  I was asking
a specific GNAT dependant technical question, simply because I would
like to know (for my own satisfaction) if I can create a true unconstrained
Ada array on the heap from C data without copying.  There is such a
thing as intellectual curiosity!

All the best,

Duncan. 

> The record type will contain the bounds, and a pointer to the data.  For
> example:
>
>      type Some_Data is record
>         First, Last: Integer;
>         Data: Some_Pointer;
>         -- may be from an instantiation of Interfaces.C.Pointers.
>      end record;
>
> Now add your own access type:
>
>     type My_Pointer is access Some_Data;
>
> Next go off and write all the code that passes objects of type Some_Data
> around.  As you do so, you may want to "reach into" your data.  Resist
> the temptation, and write inquiry functions such as:
>
>     function First_Element(P: My_Pointer) return Data_Element;
>
> and put them in your package body.  You probably also want a create
> function that calls the C routine, creates an object on the heap, and
> returns a pointer.  (Notice that the data is also on the heap but it
> doesn't get moved around.  The object you create only contains the bounds.)
>
> It sounds like a lot of work, and it will be for you at first.  But when
> you get used to it, this is a very useful design pattern in Ada.  Of
> course, if you didn't insist on putting the data on the heap, you could
> just pass objects of type Some_Data around.  In Ada you will soon find
> that this is easier than creating access types all over the place.



  reply	other threads:[~2003-10-09  9:31 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
2003-10-08 22:43         ` Robert I. Eachus
2003-10-09  9:31           ` Duncan Sands [this message]
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