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,68b2e2c0309a6e88 X-Google-Attributes: gid103376,public Path: controlnews3.google.com!news2.google.com!newsfeed2.dallas1.level3.net!news.level3.com!newsfeed.gamma.ru!Gamma.RU!newsfeed.icl.net!newsfeed.fjserv.net!news.tele.dk!news.tele.dk!small.news.tele.dk!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: Question on interface Ada to C Date: Wed, 26 May 2004 09:34:29 +0200 Organization: AdaCL Message-ID: <14104353.udXiIW5mdD@linux1.krischik.com> References: 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 1085557132 00 1515 z8KMGhqOKVG+PKf 040526 07:38:52 X-Complaints-To: usenet-abuse@t-online.de X-ID: GndhOqZHweu-4Zw26bji7Lbhi-TXtRuJeXNVZHCCrb1ThLjKEtgeck User-Agent: KNode/0.7.7 Xref: controlnews3.google.com comp.lang.ada:841 Date: 2004-05-26T09:34:29+02:00 List-Id: James Alan Farrell wrote: > Hello all, > I've inherited a project that is half written in C and Ada. Nothing wrong with that. You should carfully read Annex B. Currently I do a lot of Ada / C mixed programming as well. It works quite well. > My boss at first thought this was good code but when he took a second > look he wasn't so sure. So he's asked me to investigate. What > exactly happens with the memory used by list in this situation? The code is indeed not very good. > We're using GNAT, but I don't think that's an issue -- I would think > this code would behave (or misbehave) the same no matter what > compiler. That depends if you use GNAT with gcc's own C or GNAT with some other C. > Thanks, > James Alan Farrell > GrammaTech. > > type stuff is integer; -- just for example > > type List_Type is array(Integer range <>) of stuff; > > package MyPointers is > new System.Address_To_Access_Conversions(List_Type); > subtype List_Pointer is MyPointers.Object_Pointer; > > procedure MyProc > (Items : in out List_Pointer; > Nitems : in out Integer) is > > List : List_Type := function_that_returns_a_list; > > begin > Nitems := List'Length; > Items := MyPointers.To_Pointer(List'Address); > end; List will be short lived after MyProc ends. Also list is indefinite - C has no conzept of indefinite. You should return a record like this (Example for Character): ---------------------------------------------------------------------------- -- -- C/Ada String Type -- type C_Array is array (Natural range <>) of aliased Character; ---------------------------------------------------------------------------- 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 Data : C_Pointers.Pointer; Size : Interfaces.C.size_t; end record; With GNAT you can use function as well. Returning a record from Ada to C work OK. C_String.Data should be allocated and freed with malloc. See: http://cvs.sourceforge.net/viewcvs.py/adacl/CUnicode/Include/c-generic_strings.adb?rev=1.5&view=auto If you use GNAT only then you can import malloc directly. With Regards Martin -- mailto://krischik@users.sourceforge.net http://www.ada.krischik.com