我有以下代码片段,它展示了动态分配的矩阵(二维数组)
C语言
int** create_matrix(int r, int c)
{
int i, j, count;
int **arr = (int **)malloc(r * sizeof(int *));
for (i=0; i<r; i++)
arr[i] = (int *)malloc(c * sizeof(int));
count = 0;
for (i = 0; i < r; i++)
for (j = 0; j < c; j++)
arr[i][j] = ++count;
return arr;
}
Python (调用C函数)
r = 3
c = 3
p_int = POINTER(POINTER(c_int))
testlib.create_matrix.restype = POINTER(POINTER(c_int))
p_int = testlib.create_matrix(r, c) #does p_int and inner array deallocated automatically in python?
我的问题是:
- Python/ctypes是否处理由C分配的内存的释放?
- 如果我们需要手动释放它,应该如何操作?调用free还是其他什么?
- 任何澄清此问题的博客或文章都非常好。
p_int = POINTER(POINTER(c_int))
这一行代码呢?它并没有起到任何实际作用。 - Eryk Sunp_int
指向指针数组,也不知道数组的长度,那么它怎么知道需要调用freer+1
次呢? - Eryk Sun