Re: C chars_ptr STORAGE_ERROR in Imported function
- From: Niklas Holsti <niklas.holsti@xxxxxxxxxxxxxxx>
- Date: Sat, 23 Aug 2008 13:11:46 +0300
(Top posting by "anon" changed to bottom style...)
anon wrote:
> In <g8ltqo$7ir$1@xxxxxxxxxxxxxxx>, Kim Rostgaard Christensen
> <krc@xxxxxxxxxx> writes:
>
>>Hello there
>>
>>I am in progress of binding the pcap c library to ada, it is a
>>part of a school project. And for that i need a funtion that >>modifies a parameter to a function like so:
>>
>>char *pcap_lookupdev(char *);
>>
>>I have figured out this much:
>>
>> function pcap_lookupdev(errbuff :
Interfaces.C.Strings.Chars_Ptr)
>> return Interfaces.C.Strings.Chars_Ptr;
>> pragma Import (C, pcap_lookupdev, "pcap_lookupdev");
>>
>>This works in the sense that running it as root returns the
>>device found, but when I run it as normal user I get
>>raised STORAGE_ERROR : stack overflow (or erroneous memory
>>access)
>>
>>Do I need to declare the buffer and then then pass its c pointer
>>to the function?
anon wrote:
> Because Char_Prt in "Interfaces.C.Strings" is a private Ada
> type that C function does not understand.
No, that's wrong. RM B.3.1(1) says that
....the private type chars_ptr corresponds to a common use of "char
*" in C programs, and an object of this type can be passed to a
subprogram to which pragma Import(C,...) has been applied, and for
which "char *" is the type of the argument of the C function.
I think Kim's choice of chars_ptr for this parameter is correct.
The error was firstly in the use of an uninitialized parameter of
this type (default initialized to Null_Ptr), and secondly in using
New_Char_Array on an uninitialized char_array, making
New_Char_Array allocate a smaller new buffer than was needed to
hold the error message from pcap_lookupdev(). Thus,
pcap_lookupdev() was called with incorrect values of its parameter,
not with an incorrect type of parameter. Similar errors could have
been made in a C function calling pcap_lookupdev() with a C
parameter of type "char *".
> You need to use "Interfaces.C.Pointer" package to build a C
> Char_Ptr pointer package.
That might work, too, but Interfaces.C.Strings is the right tool
for pcap_lookupdev() since the parameter really is a NUL-terminated
C-style string.
--
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
. @ .
.
- References:
- C chars_ptr STORAGE_ERROR in Imported function
- From: Kim Rostgaard Christensen
- Re: C chars_ptr STORAGE_ERROR in Imported function
- From: anon
- C chars_ptr STORAGE_ERROR in Imported function
- Prev by Date: Re: Array slices and types
- Next by Date: Re: File output and buffering
- Previous by thread: Re: C chars_ptr STORAGE_ERROR in Imported function
- Next by thread: Re: Ada courses
- Index(es):