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=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham 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-08 15:44:08 PST Path: archiver1.google.com!news1.google.com!sn-xit-03!sn-xit-01!sn-xit-06!sn-xit-09!supernews.com!news-xfer1.atl.newshosting.com!63.218.45.11.MISMATCH!newshosting.com!news-xfer2.atl.newshosting.com!diablo.voicenet.com!prodigy.com!prodigy.com!wn14feed!worldnet.att.net!204.127.198.203!attbi_feed3!attbi.com!rwcrnsc54.POSTED!not-for-mail Message-ID: <3F849301.8090804@comcast.net> From: "Robert I. Eachus" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.0.2) Gecko/20021120 Netscape/7.01 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: C array to Ada pointer to unconstrained array without copying memory References: <3F837303.9040202@comcast.net> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 24.34.139.183 X-Complaints-To: abuse@comcast.net X-Trace: rwcrnsc54 1065653019 24.34.139.183 (Wed, 08 Oct 2003 22:43:39 GMT) NNTP-Posting-Date: Wed, 08 Oct 2003 22:43:39 GMT Organization: Comcast Online Date: Wed, 08 Oct 2003 22:43:39 GMT Xref: archiver1.google.com comp.lang.ada:503 Date: 2003-10-08T22:43:39+00:00 List-Id: Duncan Sands wrote: > 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. 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. 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. -- Robert I. Eachus "Quality is the Buddha. Quality is scientific reality. Quality is the goal of Art. It remains to work these concepts into a practical, down-to-earth context, and for this there is nothing more practical or down-to-earth than what I have been talking about all along...the repair of an old motorcycle." -- from Zen and the Art of Motorcycle Maintenance by Robert Pirsig