在使用strdup的图形C实现中无法释放内存

时间:2018-04-15 01:07:54

标签: c memory valgrind

以下是我的图表数据结构:

typedef  struct EdgeNode{
     int adjvex;
     struct EdgeNode *nextarc;
}ENode;
typedef struct VertexNode{
     char *data; 
     char *gcc;
     int  multi_gcc;
     int  is_target;
     ENode *firstarc; 
}VNode;
typedef  struct MyGraph{
     VNode vertices[100];
}Graph;

然后我首先将所有顶点的gcc字段初始化为NULL:

  if (get_pos(*pGraph, target[j],i) == -1 && target[j][0] != '\n' && target[j][0] != '\0'){
    pGraph->vertices[i].data = malloc(strlen(target[j])+1);
    strcpy(pGraph->vertices[i].data, target[j]);
    pGraph->vertices[i].gcc = NULL;
    if (j == 0)
      pGraph->vertices[i].is_target = 1;
    else{
      pGraph->vertices[i].is_target = 0;
    }
    pGraph->vertices[i].firstarc = NULL;
    pGraph->vertices[i].multi_gcc = 0;
    i++;
  }

对于图中的某些特定顶点,我将gcc字段更改为其他字符串:

else{
      new_command = 1;
      int tmp;
      tmp = get_pos(*pGraph,my_target,i); 
      char* storage = (char *)malloc(sizeof(char)*need);
      char* real_gcc = (char *)malloc(sizeof(char)*need);
      strcpy(storage,target[0]);
      for(j = 1; j < target_id; ++j) {
        strcat(storage, " " );
        strcat(storage, target[j]);
      }
      if (new_command){
      if (pGraph -> vertices[tmp].gcc == NULL){
        pGraph->vertices[tmp].gcc = strdup(storage); //valgrind error
      }else{
        printf("the gcc is %s\n",pGraph->vertices[tmp].gcc);
        strcpy(real_gcc,pGraph->vertices[tmp].gcc);
        strcat(real_gcc,"*");
        strcat(real_gcc,storage);
        pGraph->vertices[tmp].gcc = strdup(real_gcc); //valgrind error
        if (pGraph->vertices[tmp].multi_gcc == 0){
        pGraph->vertices[tmp].multi_gcc = 1;
      }
      }
    }           



      free(storage);
      free(real_gcc);
    }

valgrind给出了这样的消息,发生在我之前在代码段上标记为“valgrind error”的地方:

==1674== 28 bytes in 1 blocks are definitely lost in loss record 1 of 2
==1674==    at 0x4C2AB80: malloc (in *)
==1674==    by 0x4EBFB49: strdup (strdup.c:42)
==1674==    by 0x402343: main (mymake.c:456)
==1674== 
==1674== 56 bytes in 1 blocks are definitely lost in loss record 2 of 2
==1674==    at 0x4C2AB80: malloc (in *)
==1674==    by 0x4EBFB49: strdup (strdup.c:42)
==1674==    by 0x402422: main (mymake.c:466)

我只是没有看到内存泄漏的原因,看起来它只发生在最后一个顶点上。有什么帮助吗?

1 个答案:

答案 0 :(得分:1)

你因为你正在覆盖指向malloc内存的指针而泄漏内存:

free

在为此指针指定新值之前,需要 printf("the gcc is %s\n",pGraph->vertices[tmp].gcc); strcpy(real_gcc,pGraph->vertices[tmp].gcc); strcat(real_gcc,"*"); strcat(real_gcc,storage); free(pGraph->vertices[tmp].gcc); pGraph->vertices[tmp].gcc = strdup(real_gcc); 内存。

ndtv