C结构包含脏数据,我看不出为什么?

C结构包含脏数据,我看不出为什么?,c,struct,C,Struct,我有下面的代码,它执行对一个结构的插入,我称之为symrec,它代表symbol record symrec *createSymStruct(char const * varName, type * type, symrec ** tabella){ symrec * s; printf("creating new varibale for struct\n"); char * variableName = malloc(strlen(varName)+1); strcpy(variableN

我有下面的代码,它执行对一个结构的插入,我称之为symrec,它代表symbol record

symrec *createSymStruct(char const * varName, type * type, symrec ** tabella){
symrec * s;
printf("creating new varibale for struct\n");
char * variableName = malloc(strlen(varName)+1);
strcpy(variableName,varName);
s = getsymStruct(variableName, *tabella);
if (s == 0){
    printf("putting symbol into table\n");
    s = putsymStruct(variableName, NULL, &(*tabella));
}
s->tipo = type;
return s;
}
以及被调用的函数

symrec 
*putsymStruct(char const * identifier, type * tipo, symrec ** tabella){
  printf("\tputting symbol in the table\n");
  symrec *ptr = (symrec *) malloc (sizeof (symrec));
  ptr->name = (char *) malloc (strlen (identifier) + 1);
  strcpy (ptr->name,identifier);
  ptr->tipo = (type*)malloc(sizeof(type));
  ptr->tipo = tipo;
  ptr->next = (symrec*)tabella;
  *tabella = ptr;
  return ptr;
}
目前我只有一个这种类型的函数调用,我构建了一个函数来打印符号表的值

void readTable(symrec * tabella){
printf("PRINTING TABLE\n");
if(tabella == NULL){
    printf("table is empty\n");
}
symrec *ptr;
for (int i = 0; i < 20; i ++) {
    printf("_");
}
printf("\n");
for (ptr = tabella; ptr != (symrec *) 0;
     ptr = (symrec *)ptr->next){

    printf("|\t%*s\t|\n",20,ptr->name);
}

printf("\n");
}

这听起来不对

ptr->next = (symrec*)tabella;
也许你的意思是

ptr->next = *tabella;
而且


导致内存泄漏。前一行中从malloc返回的值丢失。

这听起来不对ptr->next=symrec*tabella;也许你的意思是ptr->next=*tabella;。同样,ptr->tipo=tipo;导致内存泄漏。上一行中从malloc返回的值丢失。两个问题1 tabella在哪里定义/分配/填充?2类型定义或其他定义在哪里?以下内容看起来可疑:ptr->tipo=type*mallocsizeoftype;,但我不能不看定义就说。最后一个问题不是你的问题,而是你应该纠正的问题——不要抛出malloc。假设类型定义正确,则:ptr->tipo=mallocsizeoftype;是正确的,避免将错误注入代码中。感谢您的建议。我回家后会尽快添加你要求的代码,因为我现在正在工作。。我不知道类型转换,因此我将在不必要的地方删除它。。顺便说一句,tabella是在一个symrec*函数中分配的,在这个函数中,除了像symrec*res这样的简单delcaration之外,什么都不会发生;返回res;好的,多亏@RSahu,你们的建议奏效了,问题确实是ptr->next=symrec*tabella;。。不知道我在想什么。。如果你把它作为答案写下来,你会得到正确的答案dude=谢谢你们两位的建议。。
ptr->next = *tabella;
ptr->tipo = tipo;