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-07 15:50:04 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!cyclone.bc.net!logbridge.uoregon.edu!msunews!not-for-mail From: "Chad R. Meiners" Newsgroups: comp.lang.ada Subject: Re: C array to Ada pointer to unconstrained array without copyingmemory Date: Tue, 7 Oct 2003 18:44:09 -0400 Organization: Michigan State University Message-ID: References: NNTP-Posting-Host: arctic.cse.msu.edu X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 Xref: archiver1.google.com comp.lang.ada:411 Date: 2003-10-07T18:44:09-04:00 List-Id: "Duncan Sands" wrote in message news:mailman.41.1065561795.25614.comp.lang.ada@ada-france.org... > On Tuesday 07 October 2003 22:53, Chad R. Meiners wrote: > > "Duncan Sands" wrote in message > > news:mailman.37.1065537708.25614.comp.lang.ada@ada-france.org... > > > > > 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. > > > > See http://www.adapower.com/lang/accessmem.html method 1c using > > Address_To_Access_Conversions. > > > > Page for strings, but it should work for any array type as long as you have > > the representation correct. > > Thanks Chad, but in fact that was not the question :) > The method you refer to makes it easy to get a local > variable with the right bounds, but that is not enough for me! > > Here is an example. > > Suppose I get the result in a local array X with the right bounds: > > type Array_Type is (Positive range <>) of Something; > type Array_Pointer is access Array_Type; > > ... > get data and length from C > ... > > X : Array_Type (1 .. length); > for X'Address use data; -- or another method > > 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). declare C_Data_Ptr : Systems.Address := To_Address(My_C_Ptr); C_Data_Length : Natural := ...; type My_Ada_Type_Element is ...; type My_Ada_Array is array(Natural range <>) of My_Ada_Type_Element; subtype My_C_Data_As_An_Ada_Array_Type is My_Ada_Array(1..C_Data_Length); package My_Converter is new Address_To_Access_Conversions(My_C_Data_As_An_Ada_Array_Type); C_Data_In_Ada : My_Ada_Array renames My_Converter.To_Pointer(C_Data_Ptr).all; -- No copying of the array done, but the array dope that you want is built from the subtype information. begin ... end;