I found it tricky to work out to access a library function that looked this:
void generate_numbers(float **out_data, size_t *size);
where generate_numbers returns allocated memory by writing an array to *out_data:
*out_data = calloc(N, sizeof(float)); // or whatever the parameter order is *out_data[0...N] = values *size = allocated bytes
The solution seems unintuitive to me, especially because cffi seems picky about precisely how C initializers are specified, and sometimes one must use a different specification from the C function signature for pointers to arrays.
I am also concerned that I have might done did it in a way that leaks memory or is vulnerable to double frees.
data = ffi.new('float *', 1) # does not work with float **, meaning of initializer not obvious size = ffi.new('size_t *', 0) library.generate_numbers(data, size)
I then wanted to access these numbers as a bytes object in Python and save them.
char_data = ffi.cast('char *', data) # does not work to ffi.buffer(data) or ffi.buffer(data) data_bytes = ffi.buffer(char_data, size)[:]