Returning C structs by-value to Julia -


i trying pass c struct julia using ccall

here file in c:

#include <stdio.h>  typedef struct {     float a;     float b; } teststruct;  teststruct getstruct() {     teststruct s = {3.0f, 5.0f};     printf("created struct a: %f b: %f\n", s.a, s.b);     return s; } 

then compile shared library use julia.

here julia file:

immutable teststruct     a::cfloat     b::cfloat end  struct = ccall((:getstruct, "libteststruct"), teststruct, ()) println("got struct a: ", struct.a, " b: ", struct.b) 

when run file expect get

created struct a: 3.000000 b: 5.000000 got struct a: 3.0 b: 5.0 

however, instead getting

created struct a: 3.000000 b: 5.000000 got struct a: 3.0 b: 0.0 

a correct b 0.

this works when use doubles in struct instead of floats, need use floats.

thank you.

if on julia v0.3.x, ccall not handle returning structs via calling convention correctly. can try changing ccall usage this:

struct_buffer = array(teststruct) ccall((:getstruct, "libteststruct"), void, (ptr{teststruct},), struct_buffer) struct = struct_buffer[] 

this issue may fixed on julia master (0.4-dev), can try , see how goes.


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -