我有以下结构和函数
// KEY
// ----------------------------
struct key {
double k1, k2;
};
// CELL
// ----------------------------
struct cell {
double x, y, h, g, rhs;
struct key *keys;
};
void cellPrintData(struct cell *c) {
printf("\n\tCELL\n\t.............\n");
printf("\t%f\n", c->x);
printf("\t%f\n", c->y);
printf("\t%f\n", c->g);
printf("\t%f\n", c->h);
printf("\t%f\n", c->rhs);
printf("\t%f\n", c->keys->k1);
printf("\t%f\n", c->keys->k2);
}
/* cellCopyValues
* ----------------------------
* Copy values from source cell
* into target cell.
*/
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;
targetcell->keys->k1 = sourcecell->keys->k1;
targetcell->keys->k2 = sourcecell->keys->k2;
}
/* cellDuplicate
* ----------------------------
* Create a duplicate cell using
* values from given cell and return it.
*/
struct cell * cellDuplicate(struct cell *c) {
struct cell *c2 = (struct cell *) malloc(sizeof(struct cell));
if (c2 == NULL) {
printf("--> Unable to malloc *c2!\n");
errno = ENOMEM;
return NULL;
}
c2->keys = (struct key *) malloc(sizeof(struct key));
if (c2->keys == NULL) {
printf("--> Unable to malloc *c2->keys!\n");
errno = ENOMEM;
return NULL;
}
cellCopyValues(c2, c);
return c2;
}
现在,从这个方法接收struct数组时遇到了一个问题:
/* cellGetNeighbors()
* ----------------------------
* Gets the neighbors of a cell
*/
struct cell * cellGetNeighbors(struct cell *c, struct cell *sstart, struct cell *sgoal, double km) {
int i;
// CREATE 8 CELLS
struct cell cn[8];
//cellPrintData(c);
for(i = 0; i < 8; i++) {
cn[i] = *cellDuplicate(c);
}
// MAKE THEM NEIGHBORS
cn[0].y -= _DISTANCETOMOVE;
cn[1].x -= _DISTANCETOMOVE;
cn[2].y += _DISTANCETOMOVE;
cn[3].x += _DISTANCETOMOVE;
cn[4].x -= _DISTANCETOMOVE;
cn[4].y -= _DISTANCETOMOVE;
cn[5].x -= _DISTANCETOMOVE;
cn[5].y += _DISTANCETOMOVE;
cn[6].x += _DISTANCETOMOVE;
cn[6].y += _DISTANCETOMOVE;
cn[7].x += _DISTANCETOMOVE;
cn[7].y -= _DISTANCETOMOVE;
// CALCULATE g, h, rhs, key
for(i = 0; i < 8; i++) {
cn[i].g = cellG(&cn[i], sgoal);
cn[i].h = cellH(&cn[i], sstart);
cn[i].rhs = _INFINITY;
cn[i].keys = cellCalculateKey(&cn[i], km);
//cellPrintData(&cn[i]);
}
// STORE THESE NEIGHBORS IN FILE.
struct cell *cptr = &cn[0];
cellPrintData(&cn[2]);
return cptr;
}
.. 进入这个方法-
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);
for(i = 0; i < 8; i++) {
// sum[i] = 0.00;
// sum[i] += cellCost(c, cn + i);
// sum[i] += cellG(cn + i, sgoal);
}
/*
// Find min sum
minsum = sum[0];
mincell = 0;
for(i = 0; i < 8; i++) {
if(minsum < sum[i]) {
minsum = sum[i];
mincell = i;
}
}
//return (cn+mincell);
*/
return cellCreateNew();
}
当我比较两个方法中的cellPrintData()输出时->
方法1:(发件人)
CELL
.............
27.203030
71.435282
34.713147
0.000050
999.000000
34.713197
34.713147
方法2:(接收器)
CELL
.............
27.203030
71.435282
34.713147
0.000050
999.000000
0.000000
0.000000
这也导致k1和k2值非常大-以及分段断层我做错什么了感谢: )
最佳答案
你的问题就在这里:
struct cell cn[8];
您正在堆栈上分配
cellGetNeighbors
,因此当您的cn
函数完成并返回时,cellGetNeighbors
的值(它的cn
版本)将不再有效,并且cellGetNeighbors
中的cn
将指向正在用于其他用途的堆栈块。你有两个简单的选择:
将8个
cellMinNeighbor
数组传递到struct cell
中,以便调用方负责分配该内存。在
cellGetNeighbors
中的堆(即cn
)上分配malloc
,并按现在的方式返回它当然,调用方在完成时必须cellGetNeighbors
返回值(这个事实应该作为free
接口的一部分记录下来)。我建议使用第二个选项,并建议构建一个单独的
cellGetNeighbors
函数来正确释放单个单元格。cellGetNeighbors
函数是个好主意,因为cellFree
函数中有一个指针,需要释放该指针当然,如果您需要使用一个不完全包含八个元素的数组,那么这将更加复杂;如果发生这种情况,那么您还必须通过向cellFree
添加一个额外的指针参数来返回数组大小如果事情发展到这一点,那么您需要添加一个单独的结构:struct cells {
int n; /* How many cells there are */
struct cell *items; /* The cells themselves */
}
以及一组函数来分配和释放这些新结构。
我猜您也有类似的堆栈和堆问题。
而且,你不需要这样做:
struct cell *cptr = &cn[0];
cellPrintData(&cn[2]);
return cptr;
struct cell
数组将在没有您干预的情况下衰减为指针,这很好:cellPrintData(&cn[2]);
return cn;
此外,由于我已经在这里写了一本书,所以您不需要将
getCellNeighbors
(或cellCalculateKey
或cn
或malloc
或任何返回calloc
的内容)的返回值转换为C,这样做可以掩盖问题所以,你说:struct cell *c2 = (struct cell *) malloc(sizeof(struct cell));
/* ... */
c2->keys = (struct keys *) malloc(sizeof(struct key));
你应该说:
struct cell *c2 = malloc(sizeof(struct cell));
/* ... */
c2->keys = malloc(sizeof(struct key));
还有一件事,你这里有个内存泄漏:
for(i = 0; i < 8; i++) {
cn[i] = *cellDuplicate(c);
}
为新的
realloc
分配的内存泄漏。你最好做点这样的事:for(i = 0; i < 8; i++) {
cellDuplicateContent(c, &cn[i]);
}
void *
只需复制单个成员,当然,还可以将cellDuplicate
的数据分配为指针(即struct cell
加上cellDuplicateContent
的分配)。关于c - C:无法接收到指向结构数组的指针,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5657268/