Hi Steven,
I just ran into the same problem. Your response to Carlos was very helpful
and worked for my code. Thanks!
- Patrick
On Saturday, February 8, 2014 6:29:28 PM UTC-5, Steven G. Johnson wrote:
>
> On Saturday, February 8, 2014 2:21:36 PM UTC-5, Carlos Becker wrote:
>
>> 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)
>>
>
> This should be quite easy. For example, return a double array, just
> allocate a double* array in C with malloc, and return it along with the
> size (if needed), then convert it to a Julia array with pointer_to_array on
> the Julia side. If you want Julia to take charge of freeing the array
> when you are done with it, pass own=true to pointer_to_array. For example:
>
> in C:
> double *myarray(int n)
> {
> double *a = malloc(sizeof(double) * n);
> for (int i = 0; i < n; ++i) a[i] = i;
> return a;
> }
>
> in Julia:
> myarray(n) = pointer_to_array(ccall((:myarray,"mylib"), Ptr{Float64},
> (Cint,), n), n, true)
>
> To return an array of structs, do the same thing, except instead of
> Float64 in Julia declare a custom immutable type that mirrors the C struct.
>