Your original code gives a deprecation warning on both Julia 0.4 and 0.3
which is actually an indication of a much deeper issue: you're converting
an array of arrays of bytes to a pointer, which gives a pointer to the data
of the top-level array data and then reinterprets that pointer as a pointer
a pointer to UInt8s. The ppl_version function will overwrite the memory at
that pointer, which should cause the first entry of versionnum to point to
some C string data instead of to a Julia array object. I suspect that if
you did this in the repl and tried to look at the versionnum object after
the ccall you would immediately get a segfault. Bottom line: arrays of
arrays are not the same as pointers to pointers.

On Tue, Feb 17, 2015 at 6:01 PM, Zenna Tavares <[email protected]>
wrote:

> The following solved this problem:
>
> [17:44] <wat_> it looks like the array is being passed directly, although
> an array of ascii strings can't be passed directly in my experience
> [17:46] <wat_> I created a function which creates an array of Ptr{UInt8}
> from an array of strings
> [17:46] <wat_> convert( ::Type{Ptr{Ptr{UInt8}}}, s::Array{ASCIIString,1}
> ) = map(pointer ,s) |> pointer
>
> On Tuesday, February 17, 2015 at 5:52:44 PM UTC-5, Zenna Tavares wrote:
>>
>> int ppl_version(const char** p)
>>
>> On Tuesday, February 17, 2015 at 5:38:36 PM UTC-5, Stefan Karpinski wrote:
>>>
>>> What is the signature of the ppl_version C function?
>>>
>>> On Tue, Feb 17, 2015 at 5:23 PM, Zenna Tavares <[email protected]>
>>> wrote:
>>>
>>>> I am attempting to wrap the Parma Polyhedron Primitive Library.
>>>>
>>>> It has a function 'ppl_version' which modifies a set of strings,
>>>> represented as a char **.  An example function which calls 'ppl_version' is
>>>> as follows:
>>>>
>>>> get_ppl_version() {
>>>>   const char* p;
>>>>   (void) ppl_version(&p);
>>>>   return p;
>>>> }
>>>>
>>>>
>>>> My Julia attempt is as follows:
>>>>
>>>> function get_ppl_version()
>>>>   versionnum = Array{Uint8}[Array(UInt8, 20) for i = 1:4]
>>>>   res = ccall((:ppl_version, "libppl_c"), Int, (Ptr{Ptr{Uint8}},),
>>>> versionnum)
>>>>   bytestring(convert(Ptr{Uint8}, versionnum[1]))
>>>> end
>>>>
>>>> However, I get gobbledygook back
>>>>
>>>> @show get_ppl_version()
>>>> =>$(Expr(:call, :ppl_version)) => "Xٮ\n"
>>>>
>>>> Actually the result is non-deterministic, evaluating it a few times
>>>> gives very strange results, e.g. ""set_bigfloat_roundin",  
>>>> "isppl_initialzied",
>>>> ec. which seems to be random mix of my julia soure-code and c code
>>>> from the api itself.  I'm not sure how that is even possible.  What am I
>>>> doing wrong?
>>>>
>>>> Thanks
>>>>
>>>
>>>

Reply via email to