有人可以告诉我 for 循环中有什么问题吗?当我运行它时,它会中断。我试图调试看看出了什么问题,我注意到在 for 循环中它只是停止了:
#define MAX_POPULATION 64
float **tr_pop;//Tournament candidates
float **matingPool;//Mating pool
tr_pop=new float *[m];
matingPool=new float *[m];
for(l=0;l<m+1;l++)//allocating
{
tr_pop[l]=new float[MAX_POPULATION];
matingPool[l]=new float[MAX_POPULATION];
}
for ( int r = 0; r < row; ++r )//deleting
{
delete [] matingPool[r];//Stops here (not ending program just frozen)
delete [] tr_pop[r];
}
delete [] tr_pop;
delete [] matingPool;
=======好的。问题解决=======
原因如下:
我只是将其更改MAX_POPULATION
为,MAX_POPULATION+1
并且它起作用了。
for(l=0;l<m+1;l++)
{
tr_pop[l]=new float[MAX_POPULATION+1];
matingPool[l]=new float[MAX_POPULATION+1];
}
因为在另一个函数中,我认为我违反了:
void crossover()
{
int p1,p2,i,j;float tempBit;
p1=m/3;
p2=(2*m)/3;
for(j=0;j<MAX_POPULATION;j++)
{
for(i=p1;i<p2;i++)
{
tempBit=matingPool[i][j];
matingPool[i][j]=matingPool[i][j+1];//THE VIOLATION POINT (I THINK)
matingPool[i][j+1]=tempBit;
}
j++;
}
如您所见,j = MAX_POPULATION
在循环结束时,我试图到达MAX_POPULATION + 1
. 所以我改变了列的分配,问题解决了:)