나는 다음과 같은 구조체 및 방법을 사용하고 있습니다 : 나는 cellMinNeighbor()
를 호출 할 때 내가 선택 기준에 따라 (cellGetNeighbors()
에서) 8 개 양산 이웃 중 하나를 반환해야특정 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]);
}
을 - 그러나 현재의 방법을하는 I 무료로 적용한 다른 요소는 다음과 같은 오류를 표시합니다.
*** glibc detected *** ./algo: free(): invalid pointer: 0x0000000001cb81c0 ***
내가 뭘 잘못하고 있니? 감사.
()는''완전히 targetcell * = * sourcecell'을 수행하면 충분하다. – Philip