comp.lang.ada
 help / color / mirror / Atom feed
From: James Alan Farrell <anonymous@anonymous.com>
Subject: Re: Question on interface Ada to C
Date: Wed, 26 May 2004 09:05:59 -0400
Date: 2004-05-26T09:05:59-04:00	[thread overview]
Message-ID: <ga59b0dlbhi0pfh2unacbq9c5c3f6h2250@4ax.com> (raw)
In-Reply-To: x7vr7t85gsg.fsf@smaug.pushface.org

Thank you for all the answers.

Why is it in out?  The quick answer is that is how it was done in the
code I inherited. 

 From what I understand, this was done to allow C to use a pointer
argument.  That does not match my understanding of how Ada works, but
I am new to Interfaces.C and do not have a good understanding of how
that works.

James Alan Farrell
GrammaTech


On 25 May 2004 22:38:07 +0100, Simon Wright <simon@pushface.org>
wrote:

>James Alan Farrell <anonymous@anonymous.com> writes:
>
>>  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;
>
>List is on the stack
>
>> 
>>    begin
>>       Nitems := List'Length;
>>       Items  := MyPointers.To_Pointer(List'Address);
>>    end;
>
>List has gone away!
>
>I think you're going to need to allocate the particular array and get
>the C code to free it (well, that's dangerous, perhaps you should
>supply an Ada subprogram to actually do the freeing so as to be sure
>the right storage pool is used; or perhaps you could import malloc?)
>
>And I don't think C will understand a List_Pointer, because List_Type
>is unconstrained so any concrete List may have bounds with it. There's
>no reason to suppose that List'Address is the same as List
>(List'First)'Address. The C is expecting effectively a System.Address,
>so make your Items parameter a System.Address (why is it in out, BTW?)
>
>One "trick" is to declare a local subtype which is constrained by the
>actual bounds.
>
>  List : List_Type := function_that_returns_a_list;
>  type actual_list_type is list_type (list'range); -- or suchlike
>  -- instantiate System.Address_To_Access_Conversions for actual_list_type
>  function malloc (n : natural) return system.address;
>  pragma import (c, malloc, "malloc");
>begin
>  Items := malloc (actual_list_type'max_size_in_storage_elements);
>  -- copy the contents of List using the System.Address_To_Access_Conversions
>  -- instantiation above
>
>Good grief, that looks complicated, I dare say I've missed something
>simple ..




  parent reply	other threads:[~2004-05-26 13:05 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 [this message]
2004-05-26 16:01     ` Martin Krischik
2004-05-26  7:34 ` Martin Krischik
replies disabled

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