簡體   English   中英

為什么我的程序在linux-gcc而不是mingw-gcc上出現段錯誤?

[英]Why my program gets a segfault in linux-gcc but not on mingw-gcc?

我已經用C語言編寫了一個程序。問題是,當我在Linux / Unix環境中編譯該程序時,出現了段錯誤。 但是,當我在Windows中使用minGW對其進行編譯時,它可以正常工作。 基本上在程序中我有一個多維數組。 在Linux / Unix中的gdb中進行編譯和調試時,我會隨機丟失多維數組的一行。 當我嘗試訪問該行中的每一列時,突然都無法訪問它們。 我的整個行迷路了。

錯誤:程序收到信號SIGSEGV,分段錯誤。

注意:isItDone(),oneToTwo()和checkAroundTwo()很可能不是問題。 我嘗試在沒有它們的情況下運行程序,但仍然顯示錯誤。

編碼:

int main( int argc, char *argv[] ){
    FILE * output;
    output = fopen("output.txt", "w");
    srand(time(NULL));
    int size = 0; // Resetting before taking arguments
    int evaltime = 0; // Resetting before taking arguments
    int rand1, rand2;
    int counter, counter2, counter3;
    if(argc == 3){
        size = atoi(argv[1]);
        evaltime = atoi(argv[2]);
    }
    else{
        return 0;
    }
    double *evaltimes = (double*)calloc(evaltime , sizeof(double)); // The array of the results we got.
    for(counter2 = 0; counter2 < evaltime; counter2++){
        int cellnumbers = 0;
        int **ground = (int**)malloc(size * sizeof(int)); //Mallocating for every iteration
        for(counter = 0; counter < size; counter++){
            ground[counter] = (int*)calloc(size, sizeof(int)); // Initializing all the 0.
        }
        while(!isItDone(ground, size)){ // It's finished when last row is 2.
            rand1 = rand() % size;
            rand2 = rand() % size; // Take two random numbers for picking in array limit.
            printf("%d %d\n", rand1, rand2);
            if(rand1 == 0){ // Is it a top cell
                if(ground[rand1][rand2] == 0){
                    ground[rand1][rand2] = 2;
                    cellnumbers++; // Increment when a cell is picked.
                }
            }
            else{
                if(ground[rand1][rand2] == 1 || ground[rand1][rand2] == 2) // The cell is already opened
                    continue;
                else{
                    ground[rand1][rand2] = 1; // Defaulting to 1 before control
                    cellnumbers++;
                    oneTotwo(ground, size, rand1, rand2);
                }
            }
        }
        if(counter2 == evaltime - 1){ // Printing the last variation
            for(counter3 = 0; counter3 < size; counter3++){
                for(counter = 0; counter < size; counter++){
                    fprintf(output, "%d  ",ground[counter3][counter]);
                }
                fprintf(output, "\n");
            }
        }
        evaltimes[counter2] = (double)cellnumbers / (double)(size*size);
        for(counter = 0; counter < size; counter++)
            free(ground[counter]);

        free(ground);
    }
    double meany = mean(evaltimes, evaltime);
    double stddeviation = stddev(evaltimes, evaltime, meany);
    fprintf(output, "mean()  =  %f \n", meany);
    fprintf(output, "stddev()  =  %f",stddeviation);  
    fclose(output);
    return 0;
}

我認為該錯誤最有可能位於main,但這是我的功能。

int isItDone(int **p, int size){
    int counter;
    for(counter = 0; counter < size; counter++){
        if(p[size - 1][counter] == 2 && (p + size - 1) != 0)
            return 1;
    }
    return 0;
}

void oneTotwo(int **p, int size, int rand1, int rand2){
    //Checking the Upper Cells
    if(rand1 < size - 1){ // Making sure no control if it is the last cell.
        if(p[rand1 + 1][rand2] == 2){
            p[rand1][rand2] = 2;
            checkAroundTwo(p, size, rand1, rand2);
        }
    }
    if(rand1 > 0){
        if(p[rand1 - 1][rand2] == 2){
            p[rand1][rand2] = 2;
            checkAroundTwo(p, size, rand1, rand2);
        }   
    }
    if(rand2  < size - 1){
        if(p[rand1][rand2 + 1] == 2){
            p[rand1][rand2] = 2;
            checkAroundTwo(p, size, rand1, rand2);
        }
    }
    if(rand2 > 0){
        if(p[rand1][rand2 - 1] == 2){
            p[rand1][rand2] = 2;
            checkAroundTwo(p, size, rand1, rand2);
        }
    }
}

void checkAroundTwo(int **p, int size, int rand1, int rand2){
    if(rand1 < size - 1){
        if(p[rand1 + 1][rand2] == 1){
            p[rand1 + 1][rand2] = 2;
            checkAroundTwo(p, size, rand1 + 1, rand2);
        }
    }
    if(rand1 > 0){
        if(p[rand1 - 1][rand2] == 1){
            p[rand1 - 1][rand2] = 2;
            checkAroundTwo(p, size, rand1 - 1, rand2);
        }
    }
    if(rand2 < size - 1){
        if(p[rand1][rand2 + 1] == 1){
            p[rand1][rand2 + 1] = 2;
            checkAroundTwo(p, size, rand1, rand2 + 1);
        }
    }
    if(rand2 > 0){
        if(p[rand1][rand2 - 1] == 1){
            p[rand1][rand2 - 1] = 2;
            checkAroundTwo(p, size, rand1, rand2 - 1);
        }
    }
}

double mean(double *p, int size){
    double sum = 0.0000000;
    int counter;
    for(counter = 0; counter < size; counter++)
        sum += p[counter];

    return sum / (double)(size);
}

double stddev(double *p, int size, double mean){
    double sum = 0.0000000;
    int counter;
    for(counter = 0; counter < size; counter++)
        sum += sqr(((double)p[counter] - mean));

    return sqrt(sum / (double)(size - 1)); 
}

double sqr(double x){
    return x*x;
}

double sqrt(double x){
    int counter = 0;
    while(sqr(counter) < x){
        counter++;
    }
    double s = counter;

    for(counter = 0; counter < 50; counter++){
        s = (double)1/2 * (double)(s + x/s);
    }
    return s;
}

讓我們假設int array [10] [10]更為奇怪的是,在gdb中,例如當我丟失array [5]行時,我嘗試從其前一行訪問它。 例如,使用array [4] [10]。 當我這樣做時,“ array [4] [10] = 49”。 為什么是這樣? 根據我的指定,Rest為(array [4] [12],array [4] [13]等)為0。 為什么突然從第四行獲得的第五行的第一個元素變為49? 我不明白。

將內存分配給2D數組時,即分配了一個指針數組,每個指針都指向一個內存塊。

這意味着在執行初始malloc您需要分配...sizeof(int*)而不是...sizeof(int)

簡而言之,更改此行:

int **ground = (int**)malloc(size * sizeof(int));

至:

int **ground = (int**)malloc(size * sizeof(int*));

暫無
暫無

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

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