comp.lang.ada
 help / color / mirror / Atom feed
From: Martin Krischik <krischik@users.sourceforge.net>
Subject: Re: Question on interface Ada to C
Date: Wed, 26 May 2004 09:34:29 +0200
Date: 2004-05-26T09:34:29+02:00	[thread overview]
Message-ID: <14104353.udXiIW5mdD@linux1.krischik.com> (raw)
In-Reply-To: rac7b0tnlsoauhcharlguhr7v3g30npk6b@4ax.com

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




      parent reply	other threads:[~2004-05-26  7:34 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-05-25 21:00 Question on interface Ada to C James Alan Farrell
2004-05-25 21:30 ` Dale Stanbrough
2004-05-25 21:38 ` Simon Wright
2004-05-26 10:23   ` Dale Stanbrough
2004-05-26 13:05   ` James Alan Farrell
2004-05-26 16:01     ` Martin Krischik
2004-05-26  7:34 ` Martin Krischik [this message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox