I got a C function like this
double* c_func(int n_rows) {
double result[n_rows];
for (int i = 0; i < n_rows; ++i) {
result[i] = (double)i;
}
return result;
}
And I use this Go function to process the C double:
// convert C double pointer to float64 slice ...
func doubleToFloats(in *C.double, length int) []float64 {
out := make([]float64, length, length)
start := unsafe.Pointer(in)
size := unsafe.Sizeof(C.double(0))
for i := 0; i < length; i++ {
val := *(*C.double)(unsafe.Pointer(uintptr(start) + size*uintptr(i)))
out[i] = float64(val)
}
return out
}
This sometimes works but sometimes not. When it not works, it return something like this:
[0 1 2 3 4 5 6 7 8 9 10 2.53e-321 3.32018606e-316 4.24664374149e-312 4.24399158193e-312 1.1383e-320 3.31882387e-316 3.71924634e-316 3.31885594e-316 3.71924634e-316 5e-324 0 4.6950308e-316 4.24664374149e-312 3.7175681e-316 3.3200616e-316]
which looks like somewhat memory issue to me...
I'm not sure if this is the correct way to handle returned double array from C in Go. If yes, how to fix the problem (which occurs ocassionally). If no, what is the correct way to handle returned double array from C?
Thanks.
OK, so I figured out a simple way to achieve this.
We first use calloc
to allocate memory for the array:
double* c_func(int n_rows) {
double* result;
result = calloc(n_rows, sizeof(double));
for (int i = 0; i < n_rows; ++i) {
result[i] = (double)i;
}
return result;
}
and after that, we simply convert the data into proper type in Go. The trick is to use C.free
to free the memory allocated from C side.
// convert C double pointer to float64 slice ...
func doubleToFloats(in *C.double, size int) []float64 {
defer C.free(unsafe.Pointer(in))
out := (*[1 << 30]float64)(unsafe.Pointer(in))[:size:size]
return out
}