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=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,b16d5240727960c4 X-Google-Attributes: gid103376,public Path: controlnews3.google.com!news1.google.com!newshub.sdsu.edu!newsfeed00.sul.t-online.de!newsmm00.sul.t-online.de!t-online.de!news.t-online.com!not-for-mail From: Martin Krischik Newsgroups: comp.lang.ada Subject: Re: C bindings, Interfaces.C.Pointers etc. Date: Thu, 13 May 2004 19:37:14 +0200 Organization: AdaCL Message-ID: <8969563.sTNc1ZJU4A@linux1.krischik.com> References: <4934218.sMSg6xXRUe@linux1.krischik.com> Reply-To: krischik@users.sourceforge.net Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7Bit X-Trace: news.t-online.com 1084471005 06 15055 6um2GV-1fH144h6 040513 17:56:45 X-Complaints-To: usenet-abuse@t-online.de X-ID: JOFhtBZfoe3lTasr0CFjUAe0wbpp6a4sGt3OV+taUwvO0bT9xWFIrd User-Agent: KNode/0.7.7 Xref: controlnews3.google.com comp.lang.ada:552 Date: 2004-05-13T19:37:14+02:00 List-Id: Dr. Adrian Wrigley wrote: > Thanks Martin for this suggestion. > > I have given (aninvolvedimation of) my code below. > > It doesn't use the I.C.Pointers package, and allows > Ada programs to access elements of the arrays using > standard array notation. The main problem is that > the bound Ada uses are over-sized, and simple use > of 'range and 'last will fall off the ends. There is one more way but only usable when you C and Ada compiler use the same memory layout for variables. The example works for Strings: type C_Array is array (Natural range <>) of aliased Character; for C_Array'Component_Size use Interfaces.C.char'Size; pragma Convention ( Convention => C, Entity => C_Array); package C_Pointers is new Interfaces.C.Pointers ( Index => Natural, Element => Character, Element_Array => C_Array, Default_Terminator => Character'First); type C_String is record -- Address of some C Array Data : C_Pointers.Pointer; -- Size of the Array. Size : Interfaces.C.size_t; end record; Ada_String : String (1 .. Natural (Some_C_String.Size)); for Ada_String 'Address use Some_C_String.Data.all'Address; The trick is in the last line. This is of corse an Unchecked_Convertion without actually moving the Data. Use at your own risk. You can probably reduce the code involved by using an normal access to Character for C_String.Data. > Overall this solution meets my needs very well, and > if I accidentally use the invalid array (upper) bound, > the program crashes very rapidly (like it would have > done in C with a similar bug!). With the trick above Ada will check the array bounds - when Size containts the right value. > I'm a little surprised that I.C.Pointers doesn't seem > to provide a convenient and efficient solution to this > problem - using C pointers directly in array access. I.C.Pointers is supposed to all the checked convertions needed and will work when C and Ada use different memory layout - at least that's the theorie. With Regards Martin. -- mailto://krischik@users.sourceforge.net http://www.ada.krischik.com