c-在一个不寻常的地方塞夫夫



我正在编写一些代码来为我的班级制作.pgm映像,当我第一次运行它时,我得到了一个segfault。我有点希望,因为众所周知,我对将最初的null结构的成员打电话给Null。但是,当我将调试陈述放入(只有数字的printf函数)中时,它给了我一个segfault。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct pgm {
    int rows;
    int cols;
    int **pixels;
} pgmPic; 
void dataCreate(pgmPic *);
void trailCreate(pgmPic *);
pgmPic *blaze(pgmPic *, char*);
int main(int argc, char** argv){
    printf("Hin");
    if(argc != 3){
        printf("Improper use of arguments.n");
        return 0;
    }
    int row, col;
    printf("point 0");
    FILE *data = fopen(argv[1], "r");
    printf("point 1");
    fscanf(data, "%dn%dn", &row, &col);
    printf("point 2");
    pgmPic *myPic = (pgmPic *) malloc(sizeof(pgmPic));
    myPic->pixels = (int**) malloc(sizeof(int)* myPic->rows);
    for(int a = 0; a < myPic->rows; a++)
        myPic->pixels[a] = (int*) malloc(sizeof(int) * myPic->cols);
    printf("point 3");
    int elev[row][col];
    for(int w = 0; w < row; w++){
        for(int v = 0; v < col; v++){
            fscanf(data, "%d", &elev[w][v]);
        }
    }
    printf("point 4");
    int min = elev[0][0];
    int max = elev[0][0];
    for(int x = 0; x < row; x++){
        for(int y = 0; y < col; y++){
            if(elev[x][y] < min)
                min = elev[x][y];
            if(elev[x][y] > max)
                max = elev[x][y];
        }
    }
    printf("point 5");
    double multiplier = (max-min)/200;
    for(int w = 0; w < row; w++){
        for(int v = 0; v < col; v++){
            elev[w][v] = elev[w][v] - min;
            elev[w][v] = elev[w][v] * multiplier;
            myPic->pixels[w][v] = 255 - elev[w][v];
        }
    }
    printf("point 6");
    myPic->rows = row;
    myPic->cols = col;
    printf("point 7");
    dataCreate(myPic);
    printf("point 8");
    myPic = blaze(myPic, argv[2]);
    printf("point 9");
    trailCreate(myPic);
    return 0;
}
void dataCreate(pgmPic *pic){
    FILE *myfile = fopen("data.pgm", "w");
    fprintf(myfile, "P2n");
    fprintf(myfile, "%d %dn", pic->cols, pic->rows);
    for(int i = 0; i < pic->rows; i++){
        for(int j = 0; j < pic->cols; j++){
            fprintf(myfile, "%3d ", pic->pixels[pic->rows][pic->cols]);
        }
        fprintf(myfile, "n");
    }
    fclose(myfile);
} 
void trailCreate(pgmPic *pic){
    FILE *myfile = fopen("data-trail.pgm", "w");
    fprintf(myfile, "P2n");
    fprintf(myfile, "%d %dn", pic->cols, pic->rows);
    for(int i = 0; i < pic->rows; i++){
        for(int j = 0; j < pic->cols; j++){
            fprintf(myfile, "%3d ", pic->pixels[pic->rows][pic->cols]);
        }
        fprintf(myfile, "n");
    } 
    fclose(myfile);
}
pgmPic *blaze(pgmPic *pic, char* dir){
    pgmPic *thing = pic;
    int row, col;
    if(strcmp(dir, "W-E") == 0){
        row = pic->rows / 2;
        col = 0;
        while(col < pic->cols){
            thing->pixels[row][col] = 0;
            col++;
            if(thing->pixels[row-1][col] <= thing->pixels[row][col] && thing->pixels[row][col] >= thing->pixels[row+1][col])
                continue;
            if(thing->pixels[row-1][col] >= thing->pixels[row][col] && thing->pixels[row-1][col] >= thing->pixels[row+1][col]){
                row--;
                continue;
            }
            if(thing->pixels[row-1][col] <= thing->pixels[row+1][col] && thing->pixels[row][col] <= thing->pixels[row+1][col]){
                row++;
                continue;
            }
        }
    }
    if(strcmp(dir, "E-W") == 0){
        row = pic->rows / 2;
        col = pic->cols - 1;
        while(col > 0){
            thing->pixels[row][col] = 0;
            col--;
            if(thing->pixels[row-1][col] <= thing->pixels[row][col] && thing->pixels[row][col] >= thing->pixels[row+1][col])
                continue;
            if(thing->pixels[row-1][col] <= thing->pixels[row+1][col] && thing->pixels[row][col] <= thing->pixels[row+1][col]){
                row++;
                continue;
            }
            if(thing->pixels[row-1][col] >= thing->pixels[row][col] && thing->pixels[row-1][col] >= thing->pixels[row+1][col]){
                row--;
                continue;
            }
        }
    }
    if(strcmp(dir, "S-N") == 0){
        col = pic->cols / 2;
        row = pic->rows - 1;
        while(col > 0){
            thing->pixels[row][col] = 0;
            row--;
            if(thing->pixels[row][col-1] <= thing->pixels[row][col] && thing->pixels[row][col] >= thing->pixels[row][col+1])
                continue;
            if(thing->pixels[row][col-1] >= thing->pixels[row][col] && thing->pixels[row][col-1] >= thing->pixels[row][col+1]){
                row--;
                continue;
            }
            if(thing->pixels[row][col-1] <= thing->pixels[row][col+1] && thing->pixels[row][col] <= thing->pixels[row][col+1]){
                row++;
                continue;
            }
        }
    }
    if(strcmp(dir, "N-S") == 0){
        col = pic->cols / 2;
        row = 0;
        while(col < pic->cols){
            thing->pixels[row][col] = 0;
            row++;
            if(thing->pixels[row][col-1] <= thing->pixels[row][col] && thing->pixels[row][col] >= thing->pixels[row][col+1])
                continue;
            if(thing->pixels[row][col-1] <= thing->pixels[row][col+1] && thing->pixels[row][col] <= thing->pixels[row][col+1]){
                col++;
                continue;
            }
            if(thing->pixels[row][col-1] >= thing->pixels[row][col] && thing->pixels[row][col-1] >= thing->pixels[row][col+1]){
                col--;
                continue;
            }
        }
    }
    return thing;
}

现在,当我运行此功能时,我希望Segfault在第2点或第3点之后发生,但是当我运行它时,我在点0点之前就得到了Segfault。您知道是什么原因造成的?

pgmPic *myPic = (pgmPic *) malloc(sizeof(pgmPic));
myPic->pixels = (int**) malloc(sizeof(int)* myPic->rows);

myPic指向新分配的内存,那么myPic->rows的值是多少?

我们不知道。

最新更新