0

私はこのコードをCで記述して、遺伝的アルゴリズムを実装しています。これは、すべての作業構造(行列とベクトル)の動的割り当てを行う部分です。これを実行すると、ときどき(5回に3回)クラッシュします。割り当てに問題がありますか?

int main()
{

const char progress[] = "|/-\\";
int n, popSize, numIter,optRoute,minDist,range,globalMin,idx;
int maxcell;
int i,j,p,t,k,d,iter,perm,index,m;
int dists[4];
int* dmat, * totalDist, * distHistory, *randomOrder, *bestOf4Route;
int** pop, ** newPop, ** tmpPop, **rtes;
unsigned int iseed = (unsigned int)time(NULL);

// Values initialization
n = 5;
range = 10;
popSize = 100;
numIter = 1*10^4;
globalMin = 100000;

srand (iseed);

//printf("Insert the number of cities: ");
//scanf("%i",&n);

printf("Matrix costs creation...");
// Creates dmat NxN matrix
// dmat è una matrice triangolare superiore con diag = 0
// declare halfsize 1D array
maxcell = ((n*n)-n)/2;
dmat = (int*)malloc(maxcell * sizeof(int));

//set array
for(i=0; i<maxcell; i++)
    dmat[i] = (rand()%10)+2;

printf("done.\n");

//print entire matrix
//print_triangmat(dmat,n);

printf("Creation of the pop matrix...");
// create popSize x N matrix
pop = (int**)malloc(n * sizeof(int*));
for (i = 0; i < n; i++) {
  pop[i] = (int*)malloc(popSize * sizeof(int));
}

printf("done.\n");
printf("Pop matrix loading...");
// charge the first row of the matrix
for (j = 0; j < n; j++) {
  pop[j][0] = j+1;
}

// charge the rest of the matrix with randperm of the first row
for (i = 0; i < popSize; i++)
{
    for (j = 0; j < n; j++)
    {
        perm = rand()%(n-j)+j;
        t = pop[perm][i];
        pop[perm][i] = pop[j][i];
        if(i!=popSize-1)
        pop[perm][i+1] = pop[j][i];
        pop[j][i] = t;
        if(i!=popSize-1);
        pop[j][i+1] = t;
    }
}
print_matrix(pop,popSize,n);

printf("done.\n");
printf("Creation of the working structures...");

// create 4 x N matrix
rtes = (int**)malloc(n * sizeof(int*));
for (i = 0; i < n; i++) {
  rtes[i] = (int*)malloc(4 * sizeof(int));
}

// Creates an array of popSize
totalDist = (int*)malloc(popSize * sizeof(int));

// Creates an array of numIter
distHistory = (int*)malloc(numIter * sizeof(int));

// Creates an array of n
bestOf4Route = (int*)malloc(n * sizeof(int));

// create 4 x N matrix
tmpPop = (int**)malloc(n * sizeof(int*));
for (i = 0; i < n; i++) {
  tmpPop[i] = (int*)malloc(4 * sizeof(int));
}

// create popSize x N matrix
newPop = (int**)malloc(n * sizeof(int*));
for (i = 0; i < n; i++) {
  newPop[i] = (int*)malloc(popSize * sizeof(int));
}

// Creates an array of popSize
randomOrder = (int*)malloc(popSize * sizeof(int));

printf("done.\n");
4

2 に答える 2

2
pop = (int**)malloc(n * sizeof(int*));
for (i = 0; i < popSize; i++) {
  pop[i] = (int*)malloc(popSize * sizeof(int));
}

popSize> nの場合、ここで問題が発生します...

于 2013-01-27T21:18:52.850 に答える
1

問題はここにあります。i = popsize-1を考えてみましょう。そうすると、無効なメモリ位置であるpop[perm][popsize]に書き込みます。次に、以下の割り当てが要求されると、要求されたメモリ位置がpop [perm] [popsize]と交差することがあるため、ランタイムエラーが発生します。

for (i = 0; i < popSize; i++) {
    for (j = 0; j < n; j++) {
        perm = rand()%(n-j)+j;
        t = pop[perm][i];
        pop[perm][i] = pop[j][i];
        if(i!=popsize-1)
        pop[perm][i+1] = pop[j][i];
        pop[j][i] = t;
        if(i!=popsize-1)
        pop[j][i+1] = t;
    }
}

一方、多くの間違ったメモリ割り当てがあります。

    pop = (int**)malloc(n * sizeof(int*));
    for (i = 0; i < popSize; i++) {
      pop[i] = (int*)malloc(popSize * sizeof(int));
    }

する必要があります

pop = (int**)malloc(n * sizeof(int*));
for (i = 0; i < n; i++) {
  pop[i] = (int*)malloc(popSize * sizeof(int));
}

 rtes = (int**)malloc(n * sizeof(int*));
    for (i = 0; i < 4; i++) {
      rtes[i] = (int*)malloc(4 * sizeof(int));
    }

する必要があります

rtes = (int**)malloc(n * sizeof(int*));
for (i = 0; i < n; i++) {
  rtes[i] = (int*)malloc(4 * sizeof(int));
}

tmpPop = (int**)malloc(n * sizeof(int*));
for (i = 0; i < 4; i++) {
  tmpPop[i] = (int*)malloc(4 * sizeof(int));
}

する必要があります

tmpPop = (int**)malloc(n * sizeof(int*));
for (i = 0; i < n; i++) {
  tmpPop[i] = (int*)malloc(4 * sizeof(int));
}

この問題にはもう1つあいまいさがあります。

numIter = 1*10^4;

^は論理XOR演算子であるため、これにより数値が14になります。本当にそうしたいですか?

于 2013-01-27T21:37:45.967 に答える