I have a 3D array that I allocate in Python and pass to a library I am writing as a type double *. In my C code, I dynamically create a separate 3D array using
double ***coords_mle = (double ***)malloc(num_coords*sizeof(double**));
for (i = 0; i < num_coords; i++) {
coords_mle[i] = (double **)malloc((end_frame-start_frame+1)*sizeof(double*));
for (j = 0; j < end_frame-start_frame+1; j++)
coords_mle[i][j] = (double *)malloc(2*sizeof(double)); }
and fill with values. I am dynamically creating the array because it depends on parameters from my python code. The array in the C library is correctly filled. I've tried to return the array to the double * pointer using either
std::copy(**coords_mle, **coords_mle + 2*3*3, coords_ret); or memcpy(coords_ret, **coords_mle, 2*3*3*sizeof(double));
(2*3*3 are the dimensions of the array). However, what I get back in python has only a few of the values I expect. The rest of the array is filled with other numbers.
What is the best way to return a 3D array from C to python using ctypes?