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.9 required=5.0 tests=BAYES_00,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-08 02:27:14 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!newsmi-us.news.garr.it!NewsITBone-GARR!news.mailgate.org!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed.icl.net!newsfeed.fjserv.net!oleane.net!oleane!news.tiscali.fr!deine.net!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: Wed, 8 Oct 2003 11:27:46 +0200 Organization: Cuivre, Argent, Or Message-ID: References: <3F837303.9040202@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 1065605165 36127 80.67.180.195 (8 Oct 2003 09:26:05 GMT) X-Complaints-To: usenet@melchior.cuivre.fr.eu.org NNTP-Posting-Date: Wed, 8 Oct 2003 09:26:05 +0000 (UTC) To: "Robert I. Eachus" , comp.lang.ada@ada-france.org Return-Path: User-Agent: KMail/1.5.1 In-Reply-To: <3F837303.9040202@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:451 Date: 2003-10-08T11:27:46+02:00 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.