From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-2.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI autolearn=unavailable autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,4947e94bd021c540 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-10-09 02:32:04 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!newsfeed.stueberl.de!teaser.fr!enst.fr!melchior!cuivre.fr.eu.org!melchior.frmug.org!not-for-mail From: Duncan Sands Newsgroups: comp.lang.ada Subject: Re: C array to Ada pointer to unconstrained array without copying memory Date: Thu, 9 Oct 2003 11:31:06 +0200 Organization: Cuivre, Argent, Or Message-ID: References: <3F849301.8090804@comcast.net> NNTP-Posting-Host: lovelace.ada-france.org Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Trace: melchior.cuivre.fr.eu.org 1065691794 381 80.67.180.195 (9 Oct 2003 09:29:54 GMT) X-Complaints-To: usenet@melchior.cuivre.fr.eu.org NNTP-Posting-Date: Thu, 9 Oct 2003 09:29:54 +0000 (UTC) To: "Robert I. Eachus" , comp.lang.ada@ada-france.org Return-Path: User-Agent: KMail/1.5.1 In-Reply-To: <3F849301.8090804@comcast.net> Content-Disposition: inline X-Virus-Scanned: by amavisd-new-20030616-p5 (Debian) at ada-france.org X-BeenThere: comp.lang.ada@ada-france.org X-Mailman-Version: 2.1.2 Precedence: list List-Id: Gateway to the comp.lang.ada Usenet newsgroup List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Xref: archiver1.google.com comp.lang.ada:525 Date: 2003-10-09T11:31:06+02:00 > > 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.