* Allocating a C string without heap
@ 2014-07-26 16:01 Victor Porton
2014-07-26 16:20 ` Dmitry A. Kazakov
0 siblings, 1 reply; 4+ messages in thread
From: Victor Porton @ 2014-07-26 16:01 UTC (permalink / raw)
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.
This would be both safer an faster.
Can you elaborate on this?
--
Victor Porton - http://portonvictor.org
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Allocating a C string without heap
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
0 siblings, 1 reply; 4+ messages in thread
From: Dmitry A. Kazakov @ 2014-07-26 16:20 UTC (permalink / raw)
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;
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Allocating a C string without heap
2014-07-26 16:20 ` Dmitry A. Kazakov
@ 2014-07-26 16:30 ` Victor Porton
2014-07-26 18:38 ` Dmitry A. Kazakov
0 siblings, 1 reply; 4+ messages in thread
From: Victor Porton @ 2014-07-26 16:30 UTC (permalink / raw)
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);?
--
Victor Porton - http://portonvictor.org
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Allocating a C string without heap
2014-07-26 16:30 ` Victor Porton
@ 2014-07-26 18:38 ` Dmitry A. Kazakov
0 siblings, 0 replies; 4+ messages in thread
From: Dmitry A. Kazakov @ 2014-07-26 18:38 UTC (permalink / raw)
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
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2014-07-26 18:38 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox