Passing a void** to C

158 views
Skip to first unread message

Dominique Orban

unread,
Oct 8, 2015, 9:36:04 PM10/8/15
to julia-users
I'm trying to call the C interface to the HSL MA97 from Julia. It's a symmetric indefinite factorization library that uses OpenMP. One of the main C functions expects a void** as argument. The example that ships with the library declares a void* variable named akeep. The main program doesn't do anything with it except that it passes &akeep (i.e., a void**) to some of the library functions. Presumably, those functions allocate memory pointed to by akeep. It's a bit perplexing, but that's how it is. At the end, there's a call of the form free(&akeep).

My question is: what syntax should I use in Julia to perform the same operations? Naively, I'm tempted to try something like

akeep = Ptr{Void}
ccall((:some_function, "libwhatever"), Void, (Ptr{Void},), akeep)

but that returns the error message: `convert` has no method matching convert(::Type{Ptr{None}}, ::Type{Ptr{None}}).

Replacing akeep with &akeep in the ccall returns: expected Ptr{None}, got Type{Ptr{None}}.

What might be the appropriate syntax here? And more importantly, is this a recipe for a segfault?'

This is with Julia 0.3.11.

Thanks!

Isaiah Norton

unread,
Oct 8, 2015, 10:08:33 PM10/8/15
to julia...@googlegroups.com
akeep = Ptr{Void}

This is declaring a "DataType" variable (try `typeof(Ptr)` to see what I mean). You need to construct an instance, so try: `akeep = Ptr{Ptr{Void}}(0)`

Then the ccall signature should be `(Ptr{Ptr{Void}},)`.

Steven G. Johnson

unread,
Oct 8, 2015, 11:58:44 PM10/8/15
to julia-users


On Thursday, October 8, 2015 at 10:08:33 PM UTC-4, Isaiah wrote:
akeep = Ptr{Void}

This is declaring a "DataType" variable (try `typeof(Ptr)` to see what I mean). You need to construct an instance, so try: `akeep = Ptr{Ptr{Void}}(0)`

Then the ccall signature should be `(Ptr{Ptr{Void}},)`.

Tony Kelman

unread,
Oct 9, 2015, 5:55:36 AM10/9/15
to julia-users
I hope you're willing/able to publish this as a package when it's ready. An HSLSparse.jl would be a great addition to JuliaSparse!

Dominique Orban

unread,
Oct 9, 2015, 7:03:51 AM10/9/15
to julia-users
Thanks for the responses! No luck so far though:

julia> akeep = Ptr{Ptr{Void}}(0)
ERROR: type cannot be constructed

I changed the signature of the ccall to Ptr{Ptr{Void}}, but declaring akeep=Ptr{Void} and passing &akeep yields: ERROR: `convert` has no method matching convert(::Type{Ptr{None}}, ::Type{Ptr{None}})

Declaring akeep=Ptr{Ptr{Void}} and passing akeep directly yields: ERROR: `convert` has no method matching convert(::Type{Ptr{Ptr{None}}}, ::Type{Ptr{Ptr{None}}})

I don't understand what there is to convert. The two types mentioned in either error message are the same...

Haven't tried 0.4 yet.

@tkelman, I'll be very happy to release this when/if I can make it work.

On Thursday, October 8, 2015 at 10:08:33 PM UTC-4, Isaiah wrote:

Tony Kelman

unread,
Oct 9, 2015, 7:11:35 AM10/9/15
to julia-users
On 0.3, try convert(Ptr{Ptr{Void}}, 0)

Dominique Orban

unread,
Oct 10, 2015, 10:37:44 AM10/10/15
to julia-users
Thanks. It's all working now.

Dominique Orban

unread,
Oct 12, 2015, 12:26:07 PM10/12/15
to julia-users
Reply all
Reply to author
Forward
0 new messages