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,start X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-10-07 07:44:10 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news.ems.psu.edu!news.litech.org!news-spur1.maxwell.syr.edu!news.maxwell.syr.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: C array to Ada pointer to unconstrained array without copying memory Date: Tue, 7 Oct 2003 16:43:00 +0200 Organization: Cuivre, Argent, Or Message-ID: NNTP-Posting-Host: lovelace.ada-france.org Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Trace: melchior.cuivre.fr.eu.org 1065537709 86121 80.67.180.195 (7 Oct 2003 14:41:49 GMT) X-Complaints-To: usenet@melchior.cuivre.fr.eu.org NNTP-Posting-Date: Tue, 7 Oct 2003 14:41:49 +0000 (UTC) To: comp.lang.ada@ada-france.org Return-Path: User-Agent: KMail/1.5.1 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:380 Date: 2003-10-07T16:43:00+02:00 Greetings. Given a C pointer Ptr to a type T (which points to a block of memory containing a C array of T's) and the length of the array Length, there are various tricks for getting an Ada array of the right size (defined on the stack - important!) that uses the block of memory to hold the array data, i.e. no copying required. For example, type Ada_Array is array (Positive range <>) of T; ... [have Length and Ptr] 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. It would be nice to have the same thing but with X dynamically allocated. For example, type Ada_Array_Pointer is access Ada_Array; ... [have Length and Ptr] X_Ptr : Ada_Array_Pointer; [do some clever stuff to set up X_Ptr to point to an array where the data is given by Ptr, and the bounds are somewhere else, but where? :)] [Pass X_Ptr around and dereference it etc.] The problem is the bounds of course. 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. Does anyone know a good way to do this? The solution only needs to work with GNAT. I appreciate that deallocating the pointer may need to be handled specially. All the best, Duncan.