我正在编写一些代码来为我的类(class)制作一个 .pgm 图像,当我第一次运行它时,我遇到了一个段错误。我有点预料到这一点,因为我对调用初始化为 NULL 的结构的成员非常不满意。但是,当我放入调试语句(只是带数字的 printf 函数)时,它在我预期之前就给了我一个段错误。
#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("Hi\n");
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, "%d\n%d\n", &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, "P2\n");
fprintf(myfile, "%d %d\n", 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, "P2\n");
fprintf(myfile, "%d %d\n", 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;
}
现在当我运行它时,我原以为段错误会出现在点 2 或 3 之后的某个地方,但是当我运行它时,我在到达点 0 之前遇到了段错误。你知道是什么原因造成的吗?
最佳答案
pgmPic *myPic = (pgmPic *) malloc(sizeof(pgmPic));
myPic->pixels = (int**) malloc(sizeof(int)* myPic->rows);
myPic
指向新分配的内存,那么myPic->rows
的值是多少?
我们不知道。
关于c - 在不寻常的地方发生段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40749126/