如何释放此结构中分配的内存
struct image_t {
char type[3];
int **ptr;
int width;
int height;
};
在第一个函数中,我进行了以下分配:
struct image_t *struktura = (struct image_t *)malloc(sizeof(struct image_t));
int **ptr = (int**)malloc(struktura->height * sizeof(int*));
for (i = 0; i < struktura->height; i++) {
*(ptr + i) = (int *)malloc(struktura->width * sizeof(int));
if (*(ptr + i) == NULL) {
break;
}
}
在第二个函数中,我必须释放已分配的内存,所以我尝试释放类似的内存,但它不起作用
void destroy_image(struct image_t **m) {
if (m != NULL) {
if (*m != NULL) {
if ((*m)->ptr != NULL) {
for (int i = 0; i < (*m)->height; i++) {
free(*((*m)->ptr + i));
}
free((*m)->ptr);
free(*m);
}
}
}
}
我不能更改destroy函数的声明,所以结构上必须有双指针。
为了使destroy函数正常工作,指针数组中的所有指针都必须是有效的或null。malloc()
返回的内存未初始化,因此脱离分配函数中的循环会使指针数组的其余部分未初始化,因而不应传递给free()
。
还要注意,您应该测试结构指针和指针数组的分配失败。此外,新分配结构的width
和height
成员是未序列化的:您应该使用函数参数来初始化它们。
destroy_image
函数可能应该在解除分配后将*m
设置为NULL
,并且即使(*m)->ptr
是空指针,也必须设置为free(*m);
。
以下是这个问题的解决方案:
- 用
calloc()
分配数组(在所有情况下都是个好主意(或 - 将CCD_ 11设置为成功分配的指针的数目
- 显式地将数组中的其余指针设置为
NULL
- 在分配失败时释放所分配的块并返回CCD_ 13
这是一个修改后的版本:
#include <stdlib.h>
struct image_t {
char type[3];
int **ptr;
int width;
int height;
};
struct image_t *allocate_image(int width, int height) {
struct image_t *struktura = calloc(1, sizeof(*struktura));
if (struktura == NULL)
return NULL;
// should initialize struktura->type too
struktura->width = width;
struktura->height = height
struktura->ptr = calloc(height, sizeof(*struktura->ptr));
if (struktura->ptr == NULL) {
free(struktura);
return NULL;
}
for (int i = 0; i < height; i++) {
struktura->ptr[i] = calloc(sizeof(*struktura->ptr[i]), width);
if (struktura->ptr[i] == NULL) {
// Iterate downwards on index values of allocated rows
// (i --> 0) is parsed as (i-- > 0)
// this test works on signed and unsigned index types, unlike (--i >= 0)
while (i --> 0) {
free(struktura->ptr[i]);
}
free(struktura->ptr);
free(struktura);
return NULL;
}
}
return struktura;
}
void destroy_image(struct image_t **m) {
if (m != NULL) {
struct image_t *p = *m;
if (p != NULL) {
if (p->ptr != NULL) {
for (int i = 0; i < p->height; i++) {
free(p->ptr[i]);
}
free(p->ptr);
}
free(p);
*m = NULL;
}
}
}
从…开始。。。
-
不要投射
malloc
返回的值 -
不要使用
*(ptr + i)
使用等效但可读性更强的版本,即ptr[i]
这样做会将您的分配更改为:
struct image_t *struktura = malloc(sizeof(struct image_t));
int **ptr = malloc(struktura->height * sizeof(int*));
for (i = 0; i < struktura->height; i++) {
ptr[i] = malloc(struktura->width * sizeof(int));
if (ptr[i] == NULL) {
break;
}
}
这是第一个问题。。。您从未将ptr
分配给任何东西。在代码块的末尾,您需要添加:
struktura->ptr = ptr;
另一个问题是struktura->height
和struktura->width
在使用时都未初始化。在使用之前,必须为它们指定一个值。
为了释放分配的内存:您当前的代码太复杂了。像free(*((*m)->ptr + i));
这样的语句包含3个指针取消引用!!。这很难读懂。我建议您使用一些局部变量来简化代码。
void destroy_image(struct image_t **m) {
if (m == NULL) return;
if (*m == NULL) return;
struct image_t *t = *m;
int **ptr = t->ptr;
if (ptr != NULL)
{
for (int i = 0; i < t->height; i++)
{
free(ptr[i]);
}
free(ptr);
}
free(t);
*m = NULL; // The only reason to pass a double pointer to this
// function is to be able to change *m. I guess
// the prototype authoe wants the function to set
// *m to NULL
}
通过使用这些局部变量,代码比free(*((*m)->ptr + i));
之类的东西更容易读取
而对于分配码中的break
。。。
奇怪的是,在前两个malloc
之后不检查NULL,然后在循环中检查。此外,使用break
有点奇怪,因为它会使其余指针未初始化。
无论如何,如果您真的想在分配代码中使用break
,那么在释放内存时也需要考虑到这一点。类似于:
void destroy_image(struct image_t **m) {
if (m == NULL) return;
if (*m == NULL) return;
struct image_t *t = *m;
int **ptr = t->ptr;
if (ptr != NULL)
{
for (int i = 0; i < t->height && ptr[i] != NULL; i++)
{
free(ptr[i]);
}
free(ptr);
}
free(t);
*m = NULL;
}
这是不必要的复杂和低效。请参阅"正确分配多维数组"。
您可以这样更改结构:
struct image_t {
char type[3];
int* ptr; // placeholder pointer
int width;
int height;
};
然后malloc像这样:
struct image_t* s = malloc(sizeof *s);
s->width = width;
s->height = height;
s->ptr = malloc( sizeof(int[width][height]) );
(记住检查每个malloc的结果,如果返回NULL,则停止程序。(
然后这样使用:
int (*arr)[s->height] = (void*) s->ptr; // convert to a temporary 2D array pointer
...
arr[i][j] = whatever; // this enables 2D array access syntax
并像这样释放它:
free(s->ptr);
free(s);