簡體   English   中英

用遞歸程序計算矩陣的行列式時發現的最奇怪的錯誤

[英]most strange bug found ever with a recursive program to calculate a determinant of a matrix

請任何人可以找到此代碼的錯誤? 我嘗試創建一個函數來計算矩陣nXn的行列式。 該程序正常運行,但是發生了“自然”的事情。 我聲明了“ char”類型的變量“ ch”,並且不再需要它了。 但是,如果刪除此變量,則會發生執行錯誤。 為什么?

/*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;
}

怎么了 如果刪除該變量導致問題,則可能是堆棧溢出。 嘗試在函數開始時將其設置為0xAB之類的值,然后在末尾將其打印出來。 如果更改,則您的代碼中有問題。

*(matriz_aux + j) = (int*) malloc( cont * sizeof(int));
/*...*/
*(*(matriz_aux + j) + aux) = *(*(matriz + 1) + k);

aux可以等於cont ,這會使緩沖區上方的第二條語句溢出。 這是未定義的行為,因此它可能會崩潰也可能不會崩潰。 任何事情都會引起變化。

您有嚴重的內存泄漏。 您正在為循環內的輔助矩陣dimension時間分配內存。 但是您只釋放了一次。 更改分配邏輯,以僅執行一次分配。

我發現這個問題。 可能在代碼末尾使用free。 我正在嘗試釋放未分配的內存。 正確的代碼是:

int determinante( int** matriz , int dimensao ){
    int cont = dimensao;
    int det = 0;
    int i, j, k, aux;
    int** matriz_aux;
    if( cont > 1 ){
        cont --;
        for( i = 0; i < dimensao; i++){
            printf(" i inicial = %d\n", 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++){
                        if( i != k ){
                            printf("valor de i = %d\n", i);
                            *(*(matriz_aux + j) + aux) = *(*(matriz + j + 1) + k);
                            aux++;
                        }
                    }
            }
            det += matriz[0][i]*pot( -1, i)*determinante(matriz_aux, cont);
        }
        /*Is here that shoud be free 'matriz_aux'*/
        for( i = 0; i < cont; i++){
            free( matriz_aux[i] );
        }
        free( matriz_aux );
    }
    else {
        det += matriz[0][0];
    }

    /*for( i = 0; i < cont; i++){
        free( matriz_aux[i] );
    }
    free( matriz_aux );*/
    return det;
}

/*here is the function 'pot'*/


int pot(int x , int y){
    int result;
    if( y == 0 ){//base case
       result = 1;
    }
    else if( y > 0 ){
        result =  x*pot( x , y-1 );//inductive step
    }
    else if( x == 0 ){
        printf("ERRO!\n");
        exit(-1);
    }
    return result;
}

感謝您的回答!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM