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-Thread: 103376,d9e66bfe9beb10b9 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news1.google.com!news.glorb.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newshosting.com!nx01.iad01.newshosting.com!newsfeed.icl.net!newsfeed.fjserv.net!feed.news.tiscali.de!newsfeed1.ip.tiscali.net!tiscali!transit1.news.tiscali.nl!dreader2.news.tiscali.nl!not-for-mail Newsgroups: comp.lang.ada Subject: Re: C array to ada record interface References: <87iscdi6m5.fsf@insalien.org> From: Ludovic Brenta Date: Mon, 26 Jul 2004 23:42:52 +0200 Message-ID: <87d62imomb.fsf@insalien.org> User-Agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux) Cancel-Lock: sha1:enhk4JO2hC3OjK+W6la8Vu18nrA= MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Organization: Tiscali bv NNTP-Posting-Date: 26 Jul 2004 23:42:20 CEST NNTP-Posting-Host: 83.134.241.154 X-Trace: 1090878140 dreader2.news.tiscali.nl 62376 83.134.241.154:32913 X-Complaints-To: abuse@tiscali.nl Xref: g2news1.google.com comp.lang.ada:2402 Date: 2004-07-26T23:42:20+02:00 List-Id: tmoran writes: > Ludovic Brenta wrote: >> (PS. I have not tried to compile my example code, so there may be >> errors in it) > > Could you post a complete, running, example please? I must have missed > something. Here is my interpretation of such a thing, but, though it > compiles on two out of three Ada compilers I tried, it generates > (different) junk results on both. (The third compiler objects to the > Unchecked_Conversion instantiation.) > > with ada.text_io, > Ada.Unchecked_Conversion, > interfaces.c; > procedure testuc2 is > > type B is new integer; > > type B_Array is array(Interfaces.c.int range <>) of aliased B; > > type B_Ptr is access all B; > pragma Convention (C, B_Ptr); > > function To_Ada_Array (Arr : in B_Ptr; Len : in Interfaces.C.int) > return B_Array is > subtype Array_With_Known_Length is B_Array (1 .. Len); > > function To_Ada is new Ada.Unchecked_Conversion > (Source => B_Ptr, Target => Array_With_Known_Length); > > begin > return To_Ada (Arr); > end To_Ada_Array; > > procedure show(ba : in b_array) is > begin > ada.text_io.put_line("ba(3)="); > ada.text_io.put_line(b'image(ba(3))); > end show; > > x : b_array(1 .. 5) := (others=>17); > begin > > show(to_ada_array(arr=>x(1)'access, len=>5)); > > end testuc2; I am the one at fault. Converting the array does not work; converting the pointer to a System.Address, and using that in a representation clause, does work. Here is my fixed code: with Ada.Text_Io, Interfaces.C; with Ada.Unchecked_Conversion; with System; procedure Testuc2 is type B is new Integer; type B_Array is array (Interfaces.C.Int range <>) of aliased B; type B_Ptr is access all B; pragma Convention (C, B_Ptr); function To_Ada_Array (Arr : in B_Ptr; Len : in Interfaces.C.Int) return B_Array is subtype Array_With_Known_Length is B_Array (1 .. Len); pragma Convention (C, Array_With_Known_Length); -- not sure this pragma is necessary function To_Address is new Ada.Unchecked_Conversion (Source => B_Ptr, Target => System.Address); Result : Array_With_Known_Length; for Result'Address use To_Address (Arr); begin return Result; end To_Ada_Array; procedure Show (Ba : in B_Array) is begin Ada.Text_Io.Put_Line ("ba(3)="); Ada.Text_Io.Put_Line (B'Image (Ba (3))); end Show; X : B_Array (1 .. 5) := (others => 17); X_Pointer : B_Ptr := X (X'First)'Access; begin Show (To_Ada_Array (Arr => X_Pointer, Len => X'Length)); end Testuc2; The representation clause ("for Result'Address use ...") specifies that the Ada array starts at the same address as the C array. This makes it possible to use Ada constructs to access elements in Result. At this point, the two strategies I outlined earlier (copy or reference) are available. -- Ludovic Brenta.