我是在Linux环境中进行编译的新手。无论如何,我在C中有一个源代码,该源代码假设是读取一系列.pgm图像文件,对其进行压缩并将压缩后的文件写入新的.pgm文件。但是,当我编译时,出现以下错误信息

*** glibc detected *** ./a.out: free(): invalid pointer: 0x00007fefc6176010 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x7a6e6)[0x7fefda73b6e6]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x6c)[0x7fefda73f9cc]
./a.out[0x408491]
./a.out[0x408565]
./a.out[0x401048]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7fefda6e230d]
./a.out[0x400a59]
======= Memory map: ========
00400000-0040b000 r-xp 00000000 07:00 299109                             /home/skyjuice/PROJECT3/a.out
0060a000-0060b000 r--p 0000a000 07:00 299109                             /home/skyjuice/PROJECT3/a.out
0060b000-0060c000 rw-p 0000b000 07:00 299109                             /home/skyjuice/PROJECT3/a.out
0060c000-059f8000 rw-p 00000000 00:00 0
05ebb000-05ee3000 rw-p 00000000 00:00 0                                  [heap]
7fefc0000000-7fefc0021000 rw-p 00000000 00:00 0
7fefc0021000-7fefc4000000 ---p 00000000 00:00 0
7fefc59d3000-7fefc59d4000 rw-p 00000000 00:00 0
7fefc5f60000-7fefc5f75000 r-xp 00000000 07:00 2117                       /lib/x86_64-linux-gnu/libgcc_s.so.1
7fefc5f75000-7fefc6174000 ---p 00015000 07:00 2117                       /lib/x86_64-linux-gnu/libgcc_s.so.1
7fefc6174000-7fefc6175000 r--p 00014000 07:00 2117                       /lib/x86_64-linux-gnu/libgcc_s.so.1
7fefc6175000-7fefc6176000 rw-p 00015000 07:00 2117                       /lib/x86_64-linux-gnu/libgcc_s.so.1
7fefc6176000-7fefda6c1000 rw-p 00000000 00:00 0
7fefda6c1000-7fefda858000 r-xp 00000000 07:00 904195                     /lib/x86_64-linux-gnu/libc-2.13.so
7fefda858000-7fefdaa57000 ---p 00197000 07:00 904195                     /lib/x86_64-linux-gnu/libc-2.13.so
7fefdaa57000-7fefdaa5b000 r--p 00196000 07:00 904195                     /lib/x86_64-linux-gnu/libc-2.13.so
7fefdaa5b000-7fefdaa5c000 rw-p 0019a000 07:00 904195                     /lib/x86_64-linux-gnu/libc-2.13.so
7fefdaa5c000-7fefdaa62000 rw-p 00000000 00:00 0
7fefdaa62000-7fefdaae5000 r-xp 00000000 07:00 904199                     /lib/x86_64-linux-gnu/libm-2.13.so
7fefdaae5000-7fefdace4000 ---p 00083000 07:00 904199                     /lib/x86_64-linux-gnu/libm-2.13.so
7fefdace4000-7fefdace5000 r--p 00082000 07:00 904199                     /lib/x86_64-linux-gnu/libm-2.13.so
7fefdace5000-7fefdace6000 rw-p 00083000 07:00 904199                     /lib/x86_64-linux-gnu/libm-2.13.so
7fefdace6000-7fefdad07000 r-xp 00000000 07:00 644102                     /lib/x86_64-linux-gnu/ld-2.13.so
7fefdaeeb000-7fefdaeee000 rw-p 00000000 00:00 0
7fefdaf04000-7fefdaf06000 rw-p 00000000 00:00 0
7fefdaf06000-7fefdaf07000 r--p 00020000 07:00 644102                     /lib/x86_64-linux-gnu/ld-2.13.so
7fefdaf07000-7fefdaf09000 rw-p 00021000 07:00 644102                     /lib/x86_64-linux-gnu/ld-2.13.so
7fff1b020000-7fff1b0ca000 rw-p 00000000 00:00 0                          [stack]
7fff1b148000-7fff1b149000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Aborted


以下是源代码片段

#定义START_FRAME 141
    #定义END_FRAME 143
    #定义SKIP_FRAME 1
    #define INPUT_STRING“ /home/image%03d.pgm”

int main(int argv, char *argc[])
{

  unsigned char *frm1, *frm2, *predict, *predictDb, *frm1E;
  unsigned char *rcnDiff, *diff, *rcnFrm1, *rcnFrm2, *wave;
  int           *xVct, *yVct, *pxE, *pyE, *code;
  int   nx  = 1920, ny = 1080, nxy;
  int   bSize  = 16, nbx, nby, nbxy;
  int   frameNo = START_FRAME;
  int   nxy  = nx * ny;

  frm1       = (unsigned char *) malloc(sizeof(unsigned char) * nxy);
  frm2       = (unsigned char *) malloc(sizeof(unsigned char) * nxy);
  rcnFrm1    = (unsigned char *) malloc(sizeof(unsigned char) * nxy);
  rcnFrm2    = (unsigned char *) malloc(sizeof(unsigned char) * nxy);
  predict    = (unsigned char *) malloc(sizeof(unsigned char) * nxy);
  predictDb  = (unsigned char *) malloc(sizeof(unsigned char) * nxy);
  diff       = (unsigned char *) malloc(sizeof(unsigned char) * nxy);
  rcnDiff    = (unsigned char *) malloc(sizeof(unsigned char) * nxy);
  wave       = (unsigned char *) malloc(sizeof(unsigned char) * nxy);
  xVct       = (int *) malloc(sizeof(int) * nbxy);
  yVct       = (int *) malloc(sizeof(int) * nbxy);

  code       = (int *) malloc(sizeof(int) * tgtBits1);
  frm1d      = (double *) malloc(sizeof(double) * nxy);
  frm2d      = (double *) malloc(sizeof(double) * nxy);
  predictd   = (double *) malloc(sizeof(double) * nxy);
  predictDbd = (double *) malloc(sizeof(double) * nxy);
  rcnFrm1d   = (double *) malloc(sizeof(double) * nxy);
  rcnFrm2d   = (double *) malloc(sizeof(double) * nxy);
  wavFrm1    = (double *) malloc(sizeof(double) * nxy);
  wavDiff    = (double *) malloc(sizeof(double) * nxy);
  wavRcnFrm1 = (double *) malloc(sizeof(double) * nxy);
  wavRcnDiff = (double *) malloc(sizeof(double) * nxy);
  diffd      = (double *) malloc(sizeof(double) * nxy);
  rcnDiffd   = (double *) malloc(sizeof(double) * nxy);

  frm1Ed     = (double *) malloc(sizeof(double) * nxE * nyE);
  frm1E      = (unsigned char *) malloc(sizeof(unsigned char) * nxE * nyE);

  //read file from PGM and store into frm1
  frameNo = START_FRAME;
  sprintf(fileName, INPUT_STRING, frameNo);
  readPgm(frm1, fileName, nx, ny);

  //after some operations

  //write into a new PGM file
   sprintf(fileName, "predictDb%03d.pgm", frameNo);
   writePgm(predictDb, fileName, nx, ny);

  return 0; //end of main function
}

//read original frame
void readPgm(unsigned char *frame, char * fileName, int nx, int ny) {
  int i, j;
  FILE *file;
  char str[128];

  file = fopen(fileName,"r");
  if (file == 0) {
    printf("Open error in File read \n");
      }
  fgets(str, 100, file);
  for(j = 0; j < ny; j++) {
      for(i = 0; i < nx; i++) {
    fscanf(file,"%d", (int *)&frame[i + nx * j]);
      }
  }
  fclose(file);
}


//create frame
void writePgm(unsigned char *frame, char *fileName, int nx, int ny){
  int i, j;
  FILE *file;

  file = fopen(fileName, "w");
  fprintf(file, "P2 %d %d 255\n", nx, ny);
  for(j = 0; j < ny; j++) {
    for(i = 0; i < nx; i++) {
      fprintf(file,"%d ", frame[i + nx * j]);
    }
    fprintf(file, "\n");
  }
  fclose(file);
}


谁能帮助您确定问题所在?谢谢

最佳答案

您永远不会初始化nxy和在调用malloc()时使用的大多数其他变量。这些变量可能包含随机值,您将无法分配正确的内存量。

关于c - 如何使用malloc读取.pgm文件并分配内存?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10168474/

10-09 07:44