C 将顶点添加到链接列表时出现问题

C 将顶点添加到链接列表时出现问题,c,linked-list,C,Linked List,我的添加顶点有问题 在valgrind中,我得到一个“未初始化的值是由堆分配创建的”错误 在0x4C27A2E:malloc(vg_替换_malloc.c:270) 通过0x400892:添加顶点(图c:56) 初始化图形 void init_graph(Graph *graph) { if (graph != NULL) graph->size = 0; } 添加顶点 int add_vertex(Graph *graph, const char new_vertex[])

我的添加顶点有问题

在valgrind中,我得到一个“未初始化的值是由堆分配创建的”错误

在0x4C27A2E:malloc(vg_替换_malloc.c:270)

通过0x400892:添加顶点(图c:56)

初始化图形

void init_graph(Graph *graph) {
  if (graph != NULL)
    graph->size = 0;
}
添加顶点

int add_vertex(Graph *graph, const char new_vertex[]) {
  Node *next, *temp, *new_node;
  int i;
  if (has_vertex(*graph, new_vertex))
    return 0;
  new_node = malloc(sizeof(Node));                  
  new_node->name = malloc(strlen(new_vertex) + 1);                      
  strcpy(new_node->name, new_vertex);                                   
  new_node->num_dest = 0;       
  if (graph->size != 0) {
    temp = graph->node;
    if (strcmp(temp->name, new_vertex) > 0) {
      new_node->next = temp;
      graph->node = new_node;
      graph->size++;
      return 1;
    }
    else for (i = 1; i < graph->size; i++) {
        next = temp->next;
        if (strcmp(next->name, new_vertex) > 0) {
          new_node->next = next;
          temp->next = new_node;
          graph->size++;
          return 1;
        }
    }
    temp->next = new_node;
    graph->size++;
    return 1;
  }
  graph->node = new_node;
  graph->size++;
  return 1;
}
int add_顶点(图*图,const char new_顶点[]){
节点*下一个,*临时,*新节点;
int i;
if(有_顶点(*图,新_顶点))
返回0;
新节点=malloc(sizeof(node));
新建节点->名称=malloc(strlen(新建顶点)+1);
strcpy(新节点->名称,新顶点);
新建节点->数量目的=0;
如果(图形->大小!=0){
temp=图形->节点;
如果(strcmp(临时->名称,新顶点)>0){
新建节点->下一步=临时;
图形->节点=新节点;
图形->大小++;
返回1;
}
(i=1;isize;i++)的else{
下一步=临时->下一步;
如果(strcmp(下一步->名称,新顶点)>0){
新建节点->下一步=下一步;
temp->next=新节点;
图形->大小++;
返回1;
}
}
temp->next=新节点;
图形->大小++;
返回1;
}
图形->节点=新节点;
图形->大小++;
返回1;
}
新增:清晰图形

void clear_graph(Graph *graph) {
  int i;
  Node *next, *n = graph->node;
  if (graph != NULL) {
    for (i = 0; i < graph->size; i++) {
      next = n->next;
      free(n);
      n = next;
    }
    free(graph);
  }
}
void clear_图(图*图){
int i;
节点*下一步,*n=图形->节点;
if(图!=NULL){
对于(i=0;isize;i++){
next=n->next;
自由(n);
n=下一个;
}
自由(图形);
}
}
附加测试

Graph graph;
  const char *vertices_to_add[]= {"koala", "platypus", "snake", "salamander",
                                  "gecko", "frog", "dog", "hedgehog"};
  int i;
init_graph(&graph);

  for (i= 0; i < sizeof(vertices_to_add) / sizeof(vertices_to_add[0]); i++)
    add_vertex(&graph, vertices_to_add[i]);

  clear_graph(&graph);
图形;
const char*顶点添加[]={“考拉”、“鸭嘴兽”、“蛇”、“蝾螈”,
“壁虎”、“青蛙”、“狗”、“刺猬”};
int i;
初始图(&图);
对于(i=0;i
好吧,您没有初始化
节点的
destcost
num_dest
字段,但这不会在
malloc
内部导致错误;我得出的结论是,该缺陷存在于未显示的代码中,可能是某种形式的内存损坏。请按照那里的说明阅读并修改您的问题。@zwol我刚刚添加了测试和它使用的所有函数,希望这有助于
graph->size=0
永远不会出现。很抱歉,您添加的代码仍然不符合要求。这既太多又不够。我们想要一个尽可能小的完整程序来演示这个bug——同时强调“尽可能小的”和“完整的程序”。
Graph graph;
  const char *vertices_to_add[]= {"koala", "platypus", "snake", "salamander",
                                  "gecko", "frog", "dog", "hedgehog"};
  int i;
init_graph(&graph);

  for (i= 0; i < sizeof(vertices_to_add) / sizeof(vertices_to_add[0]); i++)
    add_vertex(&graph, vertices_to_add[i]);

  clear_graph(&graph);