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-07 12:25:43 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: Tue, 7 Oct 2003 21:24:03 +0200 Organization: Cuivre, Argent, Or Message-ID: References: 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 1065554578 98319 80.67.180.195 (7 Oct 2003 19:22:58 GMT) X-Complaints-To: usenet@melchior.cuivre.fr.eu.org NNTP-Posting-Date: Tue, 7 Oct 2003 19:22:58 +0000 (UTC) To: tmoran@acm.org, comp.lang.ada@ada-france.org Return-Path: User-Agent: KMail/1.5.1 In-Reply-To: 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:389 Date: 2003-10-07T21:24:03+02:00 On Tuesday 07 October 2003 20:30, tmoran@acm.org wrote: > >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. > > I don't understand. Nothing is defined on, or off, the stack here. The bounds of X are on the stack if this is in a procedure; sorry I wasn't clear here (in my head X was declared as a local variable in a procedure, and that leaked out into the email). > You are saying that X is at location 12345 (or whatever) in RAM. That has no meaning - since (GNAT) X has two parts: data and bounds. The data is at location 12345, the bounds are somewhere else. > Whether X happens to be in the same area as the stack depends on > whether (System.Address_to_Access_Conversions .To_Address of) Ptr > happens to point into the stack or not. > > >It would be nice to have the same thing but with X dynamically allocated. > > If Ptr points into your program's heap memory, that's where X will be. > Or do you mean C is allocating memory, and setting its memory allocation > structures, and you want an Ada Access type, and Ada's memory allocation > structures, to be set as if that memory was allocated by an Ada "new"? Not really. The fact the memory wasn't allocated by Ada will only be problematic if it is deallocated by Ada. > >The problem is the bounds of course. > > If a call to C gave you an address and a length, then those, not a > constrained array, are what you have to work with. Just as in C, > you'll have to write things like > if Index in 1 .. Length then > Y := X(Index); > else > raise Constraint_Error; > end if; This may be what you should do - but I'm not asking that. I'm asking how to do something unwise, as in your next paragraph. > >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. > > So you're talking about using embedded assembly language to fake out the > compiler? If it's the only way to get to the moon, someone will have to > put themselves on top of an enormous can of explosives and light the > outlets. Most real problems, however, can be solved in less dangerous > ways. What's the real problem you are trying to solve here? Exactly. Well, there need be no assembly language, just some unchecked conversions. As to why, let's just say I enjoy living dangerously. It seems to me that such a trick is possible (if not desirable, and not portable between compilers) - and the degree of danger is not clear to me. If it can be done, I would like to know - that doesn't mean I'm going to do it. Anyway, someone with a good knowledge of GNAT internals should be able to say if it would work like a charm or the opposite. Thanks for your help, Duncan.