S'il vous plaît quelqu'un peut trouver un bug sur ce code? J'essaye de créer une fonction pour calculer un déterminant d'une matrice nXn. Le programme fonctionne normalement, mais une chose «sobrenatural» se produit. Je déclare la variable 'ch' de type 'char' et je n'en ai plus besoin. Mais si je supprime cette variable une occurrence d'erreur d'exécution. Pourquoi?bug le plus étrange jamais trouvé avec un programme récursif pour calculer un déterminant d'une matrice
/*the function 'pot' calculate a integer pow of a integer*/
int determinante(int** matriz , int dimensao){
char cc;/*remove this variable and execute the code. On my PC a error occurr*/
int cont = dimensao;
int det = 0;
int i, j, k, aux;
int** matriz_aux;
if(cont > 1){
cont --;
for(i = 0; i < dimensao; i++){
matriz_aux = (int**) malloc(cont * sizeof(int*));
if(matriz_aux == NULL){
printf("ERRO!\n");
exit(-3);
}
for(j = 0; j < cont; j++){
*(matriz_aux + j) = (int*) malloc(cont * sizeof(int));
if(*(matriz_aux + j) == NULL){
printf("ERRO!\n");
exit(-4);
}
}
for(j = 0; j < cont; j++){
for(k = 0, aux = 0; k < dimensao; k++){
printf("Aqui\n");
if(i != k){
printf(" i = %d\n", i);
/*I forgive to add 'j' in '*(*(matriz + 1 + j) + k). Now it works but the bug stay here*/
*(*(matriz_aux + j) + aux) = *(*(matriz + 1 + j) + k);
aux++;
}
}
}
det += matriz[0][i]*pot(-1, i)*determinante(matriz_aux, cont);
}
}
else {
det += matriz[0][0];
}
for(i = 0; i < cont; i++){
free(matriz_aux[i]);
}
free(matriz_aux);
//printf(" determinant value = %d\n", det);
return det;
}
Modifiez la syntaxe du pointeur en syntaxe de tableau. Au lieu de * (matriz_aux + j) et * (* (matriz_aux + j) + aux), utilisez matrix_aux [j] et matriz_aux [j] [aux]. Le rend beaucoup plus lisible. – Axn