【发布时间】:2016-04-14 17:43:58
【问题描述】:
我正在为 C 中的集合覆盖问题实现蚁群优化。在我的代码中,我发现了一个导致内存泄漏的函数。我很确定这个函数是内存泄漏的原因,因为我已经通过测试排除了其他函数。只是,我不明白为什么这个函数会导致内存泄漏。
为了理解这个函数,我将首先描述Ant 结构。 Ant 结构如下所示:
struct Ant {
int* x;
int* y;
int fx;
int** col_cover;
int* ncol_cover;
int un_rows;
double* pheromone;
}
typedef struct Ant ant_t;
这个结构体中的指针(如x、y、col_cover等)使用malloc初始化并在程序结束时释放。现在,导致内存泄漏的函数如下:
void localSearch(ant_t* ant) {
int improvement = 1;
ant_t* antcpy = (ant_t*) malloc(sizeof(ant_t));
initAnt(antcpy);
copyAnt(ant, antcpy);
while (improvement) {
improvement = 0;
for (int i = 0; i < inst->n; i++) {
if (antcpy->x[i]) {
removeSet(inst, antcpy, i);
while (!isSolution(antcpy)) {
constructSolution(antcpy);
}
if (antcpy->fx < ant->fx) {
copyAnt(antcpy, ant);
improvement = 1;
eliminate(ant);
} else {
copyAnt(ant, antcpy);
}
}
}
}
free((void*) antcpy);
}
首先,我使用initAnt 函数创建Ant 结构(antcpy) 的另一个实例。 copyAnt 函数将一个 Ant 结构深拷贝到另一个 Ant 结构。我做深拷贝的原因如下;我正在更改antcpy,然后将其与ant 进行比较。如果结果更好 (antcpy->fx < ant->fx),ant 将替换为 antcpy。如果结果更糟,antcpy 将恢复为 ant 的值。
这些功能如下:
void initAnt(ant_t* ant) {
ant->x = (int*) malloc(inst->n * sizeof(int));
ant->y = (int*) malloc(inst->m * sizeof(int));
ant->col_cover = (int**) malloc(inst->m * sizeof(int*));
ant->ncol_cover = (int*) malloc(inst->m * sizeof(int));
ant->pheromone = (double*) malloc(inst->n * sizeof(double));
for (int i = 0; i < inst->m; i++) {
ant->col_cover[i] = (int*) malloc(inst->ncol[i] * sizeof(int));
}
}
void copyAnt(ant_t* from, ant_t* to) {
to->fx = from->fx;
to->un_rows = from->un_rows;
for (int i = 0; i < inst->n; i++) {
to->x[i] = from->x[i];
to->pheromone[i] = from->pheromone[i];
}
for (int i = 0; i < inst->m; i++) {
to->y[i] = from->y[i];
to->ncol_cover[i] = from->ncol_cover[i];
for (int j = 0; j < inst->ncol[i]; j++) {
to->col_cover[i][j] = from->col_cover[i][j];
}
}
}
我真的不明白为什么这段代码会导致内存泄漏,因为我在localSearch 函数的末尾释放了antcpy。那么,为什么这段代码会引入内存泄漏,我该如何解决呢?
【问题讨论】:
-
您不需要
free和malloc的演员表 -
@EdHeal 把它们留在那儿有害吗?
-
你在哪里释放initAnt中分配的内存?我想 mymalloc 做了某种分配......
-
@JNevens - 你只是不需要它们
-
@AdrianRoman
mymalloc只是malloc的包装。initAnt中分配的内存不是在localSearch函数结束时通过释放antcpy来释放的吗?
标签: c struct memory-leaks