Realloc担忧

时间:2013-12-05 12:32:07

标签: c memory dynamic realloc

我正在帮助一位朋友从他的大学完成任务,并且在传递NULL指针时,realloc的行为存在道德困境(手册说它应该像普通{{1}一样工作})。

所以这是代码:

malloc

现在,重要的是:

如果我没有专门做这样的malloc

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>

typedef struct {
  char *line;
} node_t;

typedef struct {
  node_t **nodes;
  int line_count;
} buffer_t;

void print_LIFO(buffer_t buffer){
  int i;

  for(i=buffer.line_count;i>=0;i--){
    printf("%s",buffer.nodes[i]->line);
  }

}

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

  FILE *fp;

  char fline[2048];

  int i;

  buffer_t buffer;

  buffer.line_count = 0;

  if(argc < 2){
    //no params, no glory: read from STDIN

    //initial malloc so GDB and valgrind shut the f*** up
    buffer.nodes = (node_t **)malloc(sizeof(node_t *));

    while(fgets(fline,2048,stdin) != NULL){

      //do our stuff
      node_t ** tPtr = NULL;

      tPtr = (node_t **)realloc(buffer.nodes,sizeof(node_t *) * (buffer.line_count + 1));
      if(tPtr == NULL){
        //free memory!
        for(i=0;i<buffer.line_count;i++){
          free(buffer.nodes[i]->line); //free stuff alloc'ed with strdup
          free(buffer.nodes[i]); // free current node        
        }
        free(buffer.nodes);
        exit(EX_OSERR);
      }
      buffer.nodes = tPtr;
      buffer.nodes[buffer.line_count] = (node_t *)malloc(sizeof(node_t)); //alloc space for full node_t at the previously allocated node_t pointer

      if(buffer.nodes[buffer.line_count] == NULL){
        //free memory!
        for(i=0;i<buffer.line_count;i++){
          free(buffer.nodes[i]->line); //free stuff alloc'ed with strdup
          free(buffer.nodes[i]); // free current node
        } 
        free(buffer.nodes);
        exit(EX_OSERR);
      }

      buffer.nodes[buffer.line_count]->line = strdup(fline);

      buffer.line_count++;

    }   

    buffer.line_count--;

  } else {

    if(argv[1][1] == 'h'){
      printf("bocabajo: Uso: bocabajo [ fichero... ]\n");
      printf("bocabajo: Invierte el orden de las l ́neas de los ficheros (o de la entrada).\n");
      exit(EX_OK);
    }

    //check number of files to open, loop thru them
    for(i=1;i<argc;i++){
      fp = fopen(argv[i],"r");

      if(!fp){ // error check
        printf("bocabajo: Error(EX_NOINPUT),\n");
        printf("bocabajo+ El fichero \"%s\" no puede ser leido\n",argv[i]);
        exit(EX_NOINPUT);
      } 

      //initial malloc so GDB and valgrind shut the f*** up
      buffer.nodes = (node_t **)malloc(sizeof(node_t *));

      //do our magic
      while(fgets(fline,2048,fp) != NULL){

        //do our stuff
        node_t ** tPtr = NULL;

        tPtr = (node_t **)realloc(buffer.nodes,sizeof(node_t *) * (buffer.line_count + 1));
        if(tPtr == NULL){
          //free memory!
          for(i=0;i<buffer.line_count;i++){
            free(buffer.nodes[i]->line); //free stuff alloc'ed with strdup
            free(buffer.nodes[i]); // free current node
          } 
          free(buffer.nodes); // free last pointer
          exit(EX_OSERR);
        }
        buffer.nodes = tPtr;
        buffer.nodes[buffer.line_count] = (node_t *)malloc(sizeof(node_t)); //alloc space for full node_t at the previously allocated node_t pointer

        if(buffer.nodes[buffer.line_count] == NULL){
          //free memory!
          for(i=0;i<buffer.line_count;i++){
            free(buffer.nodes[i]->line); //free stuff alloc'ed with strdup
            free(buffer.nodes[i]); // free current node
          } 
          free(buffer.nodes); // free last pointer
          exit(EX_OSERR);
        }

        buffer.nodes[buffer.line_count]->line = strdup(fline);

        buffer.line_count++;

      } 

      buffer.line_count--;

      //close the file descriptor
      fclose(fp);
    }

  } 

  print_LIFO(buffer);

  //free memory
  for(i=0;i<=buffer.line_count;i++){
    free(buffer.nodes[i]->line); //free stuff alloc'ed with strdup
    free(buffer.nodes[i]); // free current node
  } 

  free(buffer.nodes);  

  exit(EX_OK);
}

在我的本地计算机上运行时,自动分配检查器会将其大脑吐出,以及Valgrind

一个例子:

//initial malloc so GDB and valgrind shut the f*** up
buffer.nodes = (node_t **)malloc(sizeof(node_t *));

Valgrind说了一句话:

*** glibc detected *** ./bocabajo: realloc(): invalid pointer: 0x000000387d221188 ***
======= Backtrace: =========
/lib64/libc.so.6[0x387d476126]
/lib64/libc.so.6(realloc+0x2e2)[0x387d47bee2]
./bocabajo[0x400a69]
/lib64/libc.so.6(__libc_start_main+0xfd)[0x387d41ecdd]
./bocabajo[0x4008f9]
======= Memory map: ========
00400000-00402000 r-xp 00000000 fd:02 1836590                            /home/jail/homefi/dep/pps/pps/2013-2014/tarea-2.2/u120182/bocabajo
00601000-00602000 rw-p 00001000 fd:02 1836590                            /home/jail/homefi/dep/pps/pps/2013-2014/tarea-2.2/u120182/bocabajo
0119e000-011bf000 rw-p 00000000 00:00 0                                  [heap]
387d000000-387d020000 r-xp 00000000 fd:00 262331                         /lib64/ld-2.12.so
387d21f000-387d220000 r--p 0001f000 fd:00 262331                         /lib64/ld-2.12.so
387d220000-387d221000 rw-p 00020000 fd:00 262331                         /lib64/ld-2.12.so
387d221000-387d222000 rw-p 00000000 00:00 0 
387d400000-387d58a000 r-xp 00000000 fd:00 262340                         /lib64/libc-2.12.so
387d58a000-387d789000 ---p 0018a000 fd:00 262340                         /lib64/libc-2.12.so
387d789000-387d78d000 r--p 00189000 fd:00 262340                         /lib64/libc-2.12.so
387d78d000-387d78e000 rw-p 0018d000 fd:00 262340                         /lib64/libc-2.12.so
387d78e000-387d793000 rw-p 00000000 00:00 0 
387e000000-387e083000 r-xp 00000000 fd:00 265418                         /lib64/libm-2.12.so
387e083000-387e282000 ---p 00083000 fd:00 265418                         /lib64/libm-2.12.so
387e282000-387e283000 r--p 00082000 fd:00 265418                         /lib64/libm-2.12.so
387e283000-387e284000 rw-p 00083000 fd:00 265418                         /lib64/libm-2.12.so
3888400000-3888416000 r-xp 00000000 fd:00 271606                         /lib64/libgcc_s-4.4.7-20120601.so.1
3888416000-3888615000 ---p 00016000 fd:00 271606                         /lib64/libgcc_s-4.4.7-20120601.so.1
3888615000-3888616000 rw-p 00015000 fd:00 271606                         /lib64/libgcc_s-4.4.7-20120601.so.1
7f48c4947000-7f48c494a000 rw-p 00000000 00:00 0 
7f48c4960000-7f48c4963000 rw-p 00000000 00:00 0 
7fff78ff2000-7fff79007000 rw-p 00000000 00:00 0                          [stack]
7fff79151000-7fff79152000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]

所以...我的问题是,为什么调试器甚至抱怨?当传递给它的指针为NULL时,手册是否指定realloc应该作为普通的malloc?

没有附加到任何类型的调试器的程序工作得很好(并且当附加确实工作正常,但调试器抱怨像我上面提到的,虽然自动分配检查器将其视为失败)。 / p>

有人能说清楚为什么会这样吗?

1 个答案:

答案 0 :(得分:0)

我认为你应该在初始malloc

之后将line_count设置为1
 buffer.nodes = malloc(sizeof(node_t *));
 buffer.line_count = 1;

我还会移除尾随的buffer.line_count--,而是在print_LIFO处理它,它看起来更强大。另外,不是传递缓冲区的副本,而是将指针传递给结构 - 如果没有别的,它应该更快。

void print_LIFO(buffer_t* buffer) 
{
  int i;

  for(i=buffer->line_count;i>0;i--)
  {
    printf("%s",buffer->nodes[i-1]->line);
  }
}

总是很好的在函数中进行一些测试,例如

void print_LIFO(buffer_t* buffer) 
{
  int i;

  if ( buffer != NULL && buffer->line_count > 0 )
  {    
    for(i=buffer->line_count;i>0;i--)
    {
      printf("%s",buffer->nodes[i-1]->line);
    }
  }
}