This is a totally reasonable thing to do. You just have to be careful
that objects are rooted across other object allocations; passing julia
objects around as Ptr{Void} hides them from the GC.If "Any" is used as the return type of the ccall, the result will be treated as a julia reference and you can skip unsafe_pointer_to_objref. A variant of this is to allocate the array in julia, and pass it to the C function to be filled in (ccall will effectively call jl_array_ptr for you to pass the array to C). On Sat, Feb 8, 2014 at 2:21 PM, Carlos Becker <[email protected]> wrote: > Hello everyone, > > I just got started with Julia, and I wanted to try to wrap a C/C++ library > to Julia to check whether it would work out for my purposes. > > I tried out many ways of passing arrays and other objects from C back to > Julia. > So far it seems that it takes a lot of extra code if I want to return, for > example, a simple double-array or an array of types (eg structs). > > Then I thought that I could call the Julia API from the ccalled binary, to > allocate an array and return it to julia, > then use unsafe_pointer_to_objref() and get a neat Julia object directly. > > You can see a very simple example here > https://gist.github.com/anonymous/8888647 > > This would simplify _significantly_ a lot of code from the C side, at least > with what I am working right now. > > Now, my question is: is it safe to call functions such as > jl_alloc_array_1d() from the C binary? > would this be a problem in some situations? > > I understand that it may mess memory up if those functions are called > outside the main thread, but I would certainly not do that. > > Thanks in advance, > Carlos
