如何正确释放某些 malloc 数组元素?
我使用以下结构和方法:
struct cell {
double x, y, h, g, rhs;
struct key *keys;
};
void cellFree(struct cell *c) {
free(c->keys);
c->keys = NULL;
free(c);
c = NULL;
}
void cellCopyValues(struct cell *targetcell, struct cell *sourcecell) {
targetcell->x = sourcecell->x;
targetcell->y = sourcecell->y;
targetcell->h = sourcecell->h;
targetcell->g = sourcecell->g;
targetcell->rhs = sourcecell->rhs;
keyCopyValues(targetcell->keys, sourcecell->keys);
}
struct cell * cellGetNeighbors(struct cell *c, struct cell *sstart, struct cell *sgoal, double km) {
int i;
// CREATE 8 CELLS
struct cell *cn = malloc(8 * sizeof (struct cell));
for(i = 0; i < 8; i++) {
cn[i].keys = malloc(sizeof(struct key));
cellCopyValues(&cn[i], c);
}
return cn;
}
struct cell * cellMinNeighbor(struct cell *c, struct cell *sstart, struct cell *sgoal, double km) {
// GET NEIGHBORS of c
int i;
struct cell *cn = cellGetNeighbors(c, sstart, sgoal, km);
double sum[8];
double minsum;
int mincell;
cellPrintData(&cn[2]);
// *** CHOOSE A CELL TO RETURN
mincell = 3; // (say)
// Free memory
for(i = 0; i < 8; i++) {
if(i != mincell) {
cellFree(&cn[i]);
}
}
return (&cn[mincell]);
}
当我调用 cellMinNeighbor()
时,我需要根据选择返回 8 个生成的邻居之一(来自 cellGetNeighbors()
)标准 - 但是,我应用于免费其他元素的当前方法似乎给了我以下错误:
*** glibc detected *** ./algo: free(): invalid pointer: 0x0000000001cb81c0 ***
我做错了什么?谢谢。
I'm using the following struct and methods:
struct cell {
double x, y, h, g, rhs;
struct key *keys;
};
void cellFree(struct cell *c) {
free(c->keys);
c->keys = NULL;
free(c);
c = NULL;
}
void cellCopyValues(struct cell *targetcell, struct cell *sourcecell) {
targetcell->x = sourcecell->x;
targetcell->y = sourcecell->y;
targetcell->h = sourcecell->h;
targetcell->g = sourcecell->g;
targetcell->rhs = sourcecell->rhs;
keyCopyValues(targetcell->keys, sourcecell->keys);
}
struct cell * cellGetNeighbors(struct cell *c, struct cell *sstart, struct cell *sgoal, double km) {
int i;
// CREATE 8 CELLS
struct cell *cn = malloc(8 * sizeof (struct cell));
for(i = 0; i < 8; i++) {
cn[i].keys = malloc(sizeof(struct key));
cellCopyValues(&cn[i], c);
}
return cn;
}
struct cell * cellMinNeighbor(struct cell *c, struct cell *sstart, struct cell *sgoal, double km) {
// GET NEIGHBORS of c
int i;
struct cell *cn = cellGetNeighbors(c, sstart, sgoal, km);
double sum[8];
double minsum;
int mincell;
cellPrintData(&cn[2]);
// *** CHOOSE A CELL TO RETURN
mincell = 3; // (say)
// Free memory
for(i = 0; i < 8; i++) {
if(i != mincell) {
cellFree(&cn[i]);
}
}
return (&cn[mincell]);
}
When I call cellMinNeighbor()
I need to return one of the 8 spawned neighbors (from cellGetNeighbors()
) based on a selection criteria - however, the current method which I've applied to the free the other elements seems to be giving me the following error:
*** glibc detected *** ./algo: free(): invalid pointer: 0x0000000001cb81c0 ***
What am I doing wrong? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在分配一个数组,然后尝试释放特定成员。
您的
cn
被分配为一个由 8 个struct cell
组成的数组,但您实际上是在尝试释放&cn[0], &cn[1] , &cn[2]
,实际上尚未使用 malloc 进行分配,而 malloc 需要它自己的空闲空间。您应该只释放那些通过 malloc 获得的指针,并且要记住的一个好规则是释放的数量必须与 malloc 的数量相对应。
在这种情况下,您 malloc cn 和各个键,但不是
&cn[1]
等。因此释放它们是一个错误。如果计算 malloc 数,则有
9
,但释放量为16
。You are allocating an array and then trying to free particular members.
Your
cn
is allocated to be an array of 8struct cell
, but you are actually trying to free&cn[0], &cn[1], &cn[2]
, which haven't actually been allocated using a malloc which requires it's own free.You should free only those pointers you got by malloc and one good rule to remember is that the number of frees must correspond to the number of mallocs.
In this case, you malloc
cn
and the individual keys, but not&cn[1]
etc. So freeing them is a mistake.If you count the mallocs, you have
9
, but frees are16
.