comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Allocating a C string without heap
Date: Sat, 26 Jul 2014 20:38:58 +0200
Date: 2014-07-26T20:38:58+02:00	[thread overview]
Message-ID: <1mxdpw9wl3x4j$.am5m405wqwdl.dlg@40tude.net> (raw)
In-Reply-To: lr0l2l$no5$1@speranza.aioe.org

On Sat, 26 Jul 2014 19:30:16 +0300, Victor Porton wrote:

> Dmitry A. Kazakov wrote:
> 
>> On Sat, 26 Jul 2014 19:01:20 +0300, Victor Porton wrote:
>> 
>>> Let we have an Ada String.
>>> 
>>> I need to pass it converted to a C string into a C library function.
>>> 
>>> Now I do it with Interfaces.C.Strings.New_String. But it is slow as it
>>> uses the heap and requires (not to forget incidentally!) further
>>> Interfaces.C.Strings.Free.
>>> 
>>> Is there a better way to do this? I mean that we would probably create a
>>> nul-terminated char_array (not on the heap but on the stack!) and pass
>>> the pointer to its first element.
>> 
>> The canonical method is this (Ada 95):
>> 
>>    procedure Foo (Text : String) is
>>       procedure Internal (Text : char_array);
>>       pragma Convention (C, Internal, "foo");
>>    begin
>>       Internal (To_C (Text));
>>    end Foo;
> 
> Why Internal (Text : char_array); not Internal (Text : char_array_access);?

char_array_access is an Ada side type. You should not pass it to C.

If you want to pass an Ada-managed array down to C it is char_array (Ada
knows how to pass arrays and record types). If you want to pass a C-managed
array it is chars_ptr.

There is no safe way to know which side manages the array. C does not make
any distinction because it is always a pointer in C. Each time you should
read the documentation carefully.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de


      reply	other threads:[~2014-07-26 18:38 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-26 16:01 Allocating a C string without heap Victor Porton
2014-07-26 16:20 ` Dmitry A. Kazakov
2014-07-26 16:30   ` Victor Porton
2014-07-26 18:38     ` Dmitry A. Kazakov [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