我很难找到这个内存泄漏。
我相信它与盆地有关
int numbasinsx(int size, double *f)
{
int maxBasin, maxRelabel, bcnt;
unsigned int newgene;
int *basin, *relabel;
int relabelcnt;
//
// find the basins
//
maxBasin = 1<<size;
basin = new int [maxBasin];
for (int gene=0; gene<maxBasin; gene++) basin[gene] = 0;
bool change = true;
bcnt = 0;
while (change) {
change = false;
for (int gene=0; gene<maxBasin; gene++) {
bool dominated = false;
for (int i=0; i<size; i++) { // for all neighbors
newgene = gene ^ (1<<i); // newgene is a neighbor 1 bit away
if (f[newgene] > f[gene]) dominated = true; // if neighbor is better mark it
if (basin[newgene]!=0) { // something to copy
if (f[newgene] > f[gene]) { // gene is dominated
if (basin[gene]==0 || basin[gene]<basin[newgene]) { // if not labeled or not lowest number
basin[gene] = basin[newgene]; // then label or relabel
change = true;
}
}
else if (f[newgene] == f[gene]) {
if (basin[gene]<basin[newgene]) {
basin[gene] = basin[newgene];
change = true;
}
}
}
}
if (! dominated && basin[gene]==0) {
// printf("max: %d %lg\n", gene, f[gene]);
basin[gene] = ++bcnt;
change = true;
}
}
}
//
// relabel the basins
//
maxRelabel = maxBasin/2+1;
relabel = new int [maxRelabel];
for (int gene=0; gene<maxRelabel; gene++) relabel[gene] = 0;
relabelcnt = 1;
for (int gene=0; gene<maxBasin; gene++) {
if (basin[gene]>=maxRelabel) printf("ERROR: trying to index %d into relabel array with max index of %d\n", basin[gene], maxRelabel-1);
if (relabel[basin[gene]]==0) {
relabel[basin[gene]] = relabelcnt++;
}
// printf("%d %lg %d\n", gene, f[gene], relabel[basin[gene]]);
}
// printf("%d\n", relabelcnt-1);
delete basin;
delete relabel;
return relabelcnt-1;
}
我将保留以上内容以帮助其他人,但带有向量的新代码现在位于http://codepad.org/KiV85Brw。向量运行时间比 delete[] 版本慢得多,而且对于大输入它甚至会出现段错误。我做错了什么