使用函数释放双指针 [英] Free double pointer using a function
问题描述
因此,我使用以下函数为双指针创建并分配了内存:
So I have created and allocated memory for a double pointer using the following function:
void mallocDoubleArr(double ***arr, int size)
{
printf("Here: %d", size);
int i, j;
*arr = malloc(size * sizeof(double*));
for(i = 0; i < size; i++)
{
(*arr)[i]= malloc(size*sizeof(double));
for (j = 0; j < size; j++)
{
(*arr)[i][j] = 0;
}
}
}
然后我使用以下函数调用了该函数:
And I called the function using:
double **G; //Create double pointer to hold 2d matrix
mallocDoubleArr(&G, numNodes);
现在我的问题是如何编写一个释放内存的函数?
Now my question is how would I write a function to free the memory?
我尝试过这样的事情:
void freeDoubleArr(double ***arr, int size)
{
int i, j;
for (i = 0; i < size; i++)
for (j = 0; j < size; j++)
free((arr)[i]);
free(arr);
}
推荐答案
似乎您想像freeDoubleArr(&G, numnodes)
一样将指针的地址传递给freeDoubleArr
(我宁愿称之为deleteDoubleArr
).那你需要拥有
It seems that you want to pass the address of a pointer to your freeDoubleArr
like freeDoubleArr(&G, numnodes)
(which I would rather call deleteDoubleArr
). Then you need to have
void freeDoubleArr(double ***arrptr, int size)
{
double** arr = *arrptr;
for (int i = 0; i < size; i++)
free(arr[i]);
free (arr);
*arrptr = NULL;
}
但是,您可以决定不将方阵表示为指向数组的指针数组,而只是将其表示为纯数组.也许使用灵活的数组成员(在C99及更高版本中)
However, you could decide that your square matrix is not represented as an array of pointers to arrays, but just as a plain array. Perhaps using flexible array members (of C99 and later) like
struct matrix_st {
unsigned size;
double arr[]; /* flexible array of size*size elements */
};
遵循arr
实际上是size*size
元素的数组(每个元素都是double
)的约定,
可能很有用.
could be useful, with the convention that arr
is really an array of size*size
elements (each being a double
).
然后,您可以定义快速访问和mutator内联函数.
Then you can define quick access and mutator inline functions.
inline double get_element(struct matrix_st *m, int i, int j) {
assert (m != NULL);
unsigned s = m->size;
assert (i>=0 && i<s && j>=0 && j<s);
return m->arr[s*i+j];
}
inline void put_element(struct matrix_st* m, int i, int j, double x) {
assert (m != NULL);
unsigned s = m->size;
assert (i>=0 && i<s && j>=0 && j<s);
m->arr[i*s+j] = x;
}
使用<assert.h>
进行优化时(请参见 assert( 3) ...)并使用-DNDEBUG
进行编译,上面的访问器get_element
和mutator put_element
可能比您的代码要快.
When optimizing and with <assert.h>
(see assert(3) ...) and compiling with -DNDEBUG
the above accessor get_element
and mutator put_element
would be perhaps faster than your code.
矩阵的创建是公正的(创建一个零位矩阵):
And the matrix creation is just (to create a zero-ed matrix):
struct matrix_st* make_matrix (unsigned size) {
struct matrix_st* m = malloc(sizeof (struct matrix_st)
+ size*size*sizeof(double);
if (!m) { perror("malloc"); exit(EXIT_FAILURE); };
m->size = size;
memset(m->arr, 0, sizeof(double)*size*size);
return m;
}
然后,用户只需使用一次调用free
即可释放这样的矩阵.
Then the user could just use one single call to free
to free such a matrix.
BTW,如果在Linux上进行编码,请使用gcc -Wall -g
进行编译,并使用 valgrind 内存泄漏检测器和 gdb 调试器.
BTW, if coding on Linux, compile with gcc -Wall -g
and use valgrind memory leak detector and gdb debugger.
这篇关于使用函数释放双指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!