error info:
.\tools.c(17775): error C2069: Cast from "void" to non-void .\tools.c(17775): error C2036: "void *" : unknown size
my code:
This function attempts to free the memory of one-dimensional array or two-dimensional array, but do not work.
cdef void free_memory(void* arr, int ndim, int rows):
cdef int i
if ndim == 1:
free(<double*>arr)
elif ndim == 2:
for i in range(rows):
free(<double*>arr[i])
free(<double**>arr)
else:
raise ValueError("Unsupported number of dimensions (ndim must be 1 or 2).")
example:
cdef double** array = <double**> malloc(n * sizeof(double*))
free_memory(array , 2, n)
I have tried to modify it many times, including using gpt assistance, but it still doesn't work.
This:
free(<double*>arr[i])
is attempting to index a void*
, and then cast the result to the type you're expecting. But you can't index a void*
. The compiler doesn't have any information about what type arr
points to.
You're casting too late. What you need to do is cast arr
itself to the right type, and then index that:
free((<double**>arr)[i])
Aside from that, if you want to free the row pointers, you need to actually have row pointers. Your example:
cdef double** array = <double**> malloc(n * sizeof(double*))
free_memory(array , 2, n)
doesn't actually allocate any memory for the rows.