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 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-09 15:59:35 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.vmunix.org!newspeer1-gui.server.ntli.net!ntli.net!newsfep1-gui.server.ntli.net.POSTED!53ab2750!not-for-mail From: "Steve Adams" Newsgroups: comp.lang.ada References: <0TUgb.639$2_2.29@newsfep1-gui.server.ntli.net> Subject: Re: C array to Ada pointer to unconstrained array without copyingmemory 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 X-Inktomi-Trace: cpc1-fare1-3-0-cust50.cos2.cable.ntl.com 1065740373 610 81.96.32.50 (9 Oct 2003 22:59:33 GMT) Message-ID: Date: Thu, 9 Oct 2003 23:59:19 +0100 NNTP-Posting-Host: 80.1.224.4 X-Complaints-To: abuse@ntlworld.com X-Trace: newsfep1-gui.server.ntli.net 1065740373 80.1.224.4 (Thu, 09 Oct 2003 23:59:33 BST) NNTP-Posting-Date: Thu, 09 Oct 2003 23:59:33 BST Organization: ntl News Service Xref: archiver1.google.com comp.lang.ada:561 Date: 2003-10-09T23:59:19+01:00 List-Id: Not sure, let me answer a different question :) Fat pointer is, and forgive me for using C, but otherwise ada would need type definitions for lots of things typedef struct { int *bounds; T *data; } Fat_Pointer; Then when you declare: fp : Fat_Pointer := NULL; it means that the pointers are actually null, the fp variable is the size of the structure above, 2 32 bit values for ease. when you do: fp := new t(1..20); then what happens in essence is: allocate enough memory for 20*sizeof(T) + 2 * sizeof(int) (for bounds) fp.bounds = 20*sizeof(T) + 2 * sizeof(int) fp.bounds(1) := 1; fp.bounds(2) := 20; fp.data = (T*)(fp.bounds+2); -- O/S conversion, but basically move 2 int sizes along in memory When you deallocate you release the memory pointed to. To take this to the C - Ada chat level, and using a thin pointer because of 'size applied: void cf() { int *data; data = malloc((N_ELEMENTS + 2)*sizeof(int); data[0] = 1; data[1] = N_ELEMENTS; data += 2; for (i =0; i < N_ELEMENTS; i++) { data[i] = i; } ada_func(data, data); } ------ procedure ada_func(p_ia_1 : int_arr_ptr; p_ia_2 : in out int_arr) is begin for i in p_ia_1'first .. p_ia_1'last loop text_io.put_line("p_ia_1 (" & integer'image(i) & "} = " & integer'image(p_ia_1(i)) ); end loop; end ada_func; I actually have code that does this and works (well something more useful anway). Objectada uses thin pointers by default. btw. Freeing a fat pointer with unchecked_deallocation is a good question, I would guess that it does free correctly. However its any ones guess outside of Gnat whether the data is freed in one call or as two. If two I am in trouble :) However, every time i traced the use of these things the bounds were ALWAYS immediately before the data in memory, suggesting a single allocation from the heap. If you use a thin pointer then I'd be pretty sure that you will always have correct behavour as by definition the bounds must reside at the start of the data in someway. "Duncan Sands" wrote in message news:mailman.51.1065630515.25614.comp.lang.ada@ada-france.org... > Hi Steve, thanks for the interesting information. > > > For infomration the Fat pointer is a struct with two pointers, one to the > > bounds and one to the data. Surprise is that the layout of the bounds and > > data in memory is the same, so they are allocated in a single call, this > > means thin and fat are the same at the basest level, you just have more > > overhead for fats. > > Do you mean that the bounds are also stored directly before the data in > memory? If so, it should still be possible to have the two pointers point > to quite different memory areas - after all, when you use an address clause > to say that the data is somewhere, the bounds will be on the stack and thus > not immediately before the data. Does this mean that if you free a fat > pointer using Unchecked_Deallocation, then only the memory pointed to by > the bounds part is freed? (Because the memory is allocated as one block, > the bounds point to the start, the data a bit further along). > > Thanks again for your help - I'm glad someone understood my question. > > All the best, > > Duncan.