分段故障:C终端中有11条错误消息



在终端中编译时,我不断收到错误Segmentation Fault:11。目标是在寄宿生的照片上画黑圈,等等。我的理由是,如果因为我的文件IO,它不起作用;out FILE类型,并将pgmUtility中调用的两个函数更改为不调用文件,程序运行顺利。所以我想我需要帮助,专注于我的文件IO的问题。

使用的命令:

$/a.out-c 470 355 100<balls.asci.pgm>TestImages/balls_c100_4.pgm

它使用与pgmUtility.c 相关的main.c程序

这是Main.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "pgmUtility.h"
#define ROWS 4
#define COLS 100
void usage(void) 
{
   printf("Usagen");
   printf(" -h Help Dialogn");
   printf(" -e edgeWidth < OldImageFile > NewImageFilen");
   printf(" -c centerRow centerCol radius < OldImageFile > NewImageFilen");
   printf(" -e edgeWidth -c radius centerRow centerCol < OldImageFile > NewImageFilen");
   exit (8);
}

int main(int argc, char * argv[]) {
   FILE *fp;
   FILE *out;
   int i, j;
   int flag1 = 0; //-e switch (edge draw)
   int flag2 = 0; //-c switch (circle draw)
   int numRows, numCols, centerRow, centerCol, radius, edgeWidth;
   char originalImage[100], newImageFile[100];

   char **header = (char**) malloc (sizeof(char*)*4);
   int **pixels;

  //command line argument parsing
  //turn flag switches on or off
   if(argc < 3)
      usage();
   if(argc > 7)
      usage();
   for(i = 1; i < argc; i++) {
      if(strncmp(argv[i], "-e", 2) == 0) {
      //set flag on
      //get edge with values)
         if(atoi(argv[i+1]) == 0) {
            usage();
         }
         edgeWidth = atoi(argv[i+1]);
         if(argv[i+2] != NULL) {
            if(atoi(argv[i+2]) != 0) {
               usage();
            }
         }
         flag1 = 1;
      }
      if(strncmp(argv[i], "-c", 2) == 0) {
      //set flag on
      //get radius and center values
         if(atoi(argv[i+1]) == 0) {
            usage();
         }
         centerRow = atoi(argv[i+1]);
         centerCol = atoi(argv[i+2]);
         radius = atoi(argv[i+3]);
         flag2 = 1;
         strcpy(originalImage, argv[5]);
         strcpy(newImageFile, argv[6]);
         fp = fopen(originalImage, "r");
         out = fopen(newImageFile, "w");
      }
      if(strncmp(argv[i], "-h", 2) == 0) {
         usage();
      }
   }
  //allocate memory for header array
   header = (char **)malloc(ROWS * sizeof(char));
   for(i = 0; i < ROWS; i++) {
      for(j = 0; j < COLS; j++) {
         header[i] = (char *)malloc(COLS * sizeof(char *));
      }
   }
  //read pgm file
   pixels = pgmRead(header, &numRows, &numCols, fp);
   if(pixels == NULL)
      usage();
   switch(flag1) {
      case 1 :
         if(flag2 == 1) {
         //execute circle draw and edge draw
            pgmDrawCircle(pixels, numRows, numCols, centerRow, centerCol, radius, header);
            pgmDrawEdge(pixels, numRows, numCols, edgeWidth, header);
         }
         else { 
         //execute only edge draw only
            pgmDrawEdge(pixels, numRows, numCols, edgeWidth, header);
         }
         break;
      case 0 :
         if(flag2 == 1) {
         //execute circle draw
            pgmDrawCircle(pixels, numRows, numCols, centerRow, centerCol, radius, header);
         }
         break;
      default : 
         usage();
         break;
   }

  //write new pgm file
   pgmWrite((const char **)header, (const int **)pixels, numRows, numCols, out);
  //Garbage Collection
  //Fix this
  //free(pixels);
  //free(header);
   for(i = 0; i < numRows; i++) {
      int *current= pixels[i];
      free(current);
   }
   for(i = 0; i < ROWS; i++) { 
      char *current = header[i];
      free(current);
   }
   return 0;
}

这是pgmUtility.c中的两个函数,我认为这可能是问题的原因。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "pgmUtility.h"
#define ROWS 4
#define COLS 100

// Implement or define each function prototypes listed in pgmUtility.h file.
// NOTE: You can NOT change the input, output, and argument type of the functions in pgmUtility.h
// NOTE: You can NOT change the prototype (signature) of any functions listed in pgmUtility.h
int ** pgmRead( char **header, int *numRows, int *numCols, FILE *in  ){
    int r, c;
  int **array;
  for(r = 0; r < ROWS; r++) {
    fgets(header[r], COLS, stdin);
    if(header == NULL)
      return NULL;
  }
  //sscanf parses the numRows and numCols
  sscanf(header[ROWS - 2], "%d %d", numCols, numRows);
  //read in pixel map
  array = (int **)malloc(*numRows * sizeof(int *));
  for(r = 0; r < *numRows; r++) {
    array[r] = (int *)malloc(*numCols * sizeof(int));
  }
  for(r = 0; r < *numRows; r++) {
    for(c = 0; c < *numCols; c++) {
      fscanf(in, "%d", *(array + r) + c );
    }
  }
   fclose(in);
  return array;
}

int pgmWrite( const char **header, const int **pixels, int numRows, int numCols, FILE *out ){

//iterate straight through pixels
//setup with a loop to insert a new line every "numCols" and keep printing until "numRows + 1" is reached (as soon as numRows + 1   break loop)
   int i, j;
   for(i = 0; i < 4; i++){
    //printf("%s", *header[i]);
      fprintf(out, "%c", *header[i]);
   }
    //for(i = 0; i < 4; i++)
            //fprintf(out, "*I=%d**%s**", i, header[i]);

   for(j = 0; j < numRows; j++){
      for(i = 0; i < numCols; i++)
         fprintf(out, "%d ", pixels[i][j]);
      fprintf(out, "n");
   }
   fclose(out);
   return 0;

}

您没有正确分配header数组。应该是:

header = malloc(ROWS * sizeof(char*));
for(i = 0; i < ROWS; i++) {
    header[i] = malloc(COLS * sizeof(char));
}

您在两个sizeof调用中使用了错误的类型。

您根本不需要内部的j循环,而是重复分配给同一个header[i]

对于C(但不是C++),请参阅:我是否强制转换malloc的结果?

此外,在main()开始时,您有一个从未使用或免费的额外分配:

char **header = (char**) malloc (sizeof(char*)*4);

你应该把它扔掉。

这与错误无关,但这是错误的:

if(header == NULL)
    return NULL;

您应该测试header[r]

为了清晰起见,我建议重写:

  fscanf(in, "%d", *(array + r) + c );

作为:

  fscanf(in, "%d", &array[r][c]);

它显示(即使按照正确建议更正了malloc调用之后),您正在分配header[i] COLS的次数。

header = malloc(ROWS * sizeof(char*));
for(i = 0; i < ROWS; i++) {
  for(j = 0; j < COLS; j++) {
     header[i] = malloc(COLS * sizeof(char));  // this happens COLS times
  }
}

这将留下每个分配的header[i]COLS编号。当我阅读您的代码时,您只需要为每个header[i]分配一个char数组。为此,您需要将header[i] = malloc(COLS * sizeof(char));移动到for(j = 0; j < COLS; j++)循环之外:

header = malloc(ROWS * sizeof(char*));
for(i = 0; i < ROWS; i++) {
    header[i] = malloc(COLS * sizeof(char));
}

您还应该验证header和每个header[i]是否已成功分配。

相关内容

  • 没有找到相关文章

最新更新