0

The title of this question may be a little confusing.

I want to call a C function in python with cffi. It's a compression function. Part of the function looks like this:

int compression_float_3d(float* array, int nx, int ny, int nz)
{
...
void* buffer;
...
// allocate buffer for compressed data
buffer = malloc(bufsize);
...
// compress the array
size = compress(...);
}

In this function, the compressed array is stored in a buffer pointer. Before I call the function, I won't know the size of the compressed array, which I will know after calling it. I want to get the compressed array and don't want to write it into the file. Therefore, I am thinking how to get the buffer pointer from the function. I don't know what I should pass to the function. Or should I change the return type of this function?

  • I've seen, with about the same frequency, either passing a `**buffer` (and returning `size`) or passing `*size` to the function (and returning `*buffer`). – jedwards Jun 29 '18 at 20:11
  • @jedwards I have been thinking about the second way. But how to deal with this kind of situation that the return value is a pointer? And in the first method, why do I need to pass a `**buffer` instead of `*buffer`. – Zhicheng Geng Jun 29 '18 at 20:31
  • Returning a pointer is not a huge issue ([similar example](https://stackoverflow.com/questions/7754986/returning-pointer-from-a-function)). I suggested `**buffer` so you could, inside your function, dereference the double pointer and "modify" the pointer-to-buffer, in effect "returning" it. – jedwards Jun 29 '18 at 20:50
  • @jedwards Yeah sure I know how to return a pointer. What I am confused is I will call this function in python. In c, I just need to do `float* foo = compression()`. But what should I do in python to get the pointer? – Zhicheng Geng Jun 29 '18 at 21:00
  • You seem to be conflating two issues. You should first decide how to make the C function (as if it was called by some other C code); and then once you have decided, post it here and ask how you can call it with CFFI from Python. – Armin Rigo Jun 30 '18 at 07:11

0 Answers0