0

这是我的代码的一部分......我有几个主要问题,我坚持......好吧,到目前为止我可以看到的大部分问题都已修复......但我仍然不知道在哪里释放内存。这是我结束程序时的错误消息:S

 HEAP SUMMARY:
==15468==     in use at exit: 20 bytes in 1 blocks
==15468==   total heap usage: 1 allocs, 0 frees, 20 bytes allocated
==15468== 
==15468== 20 bytes in 1 blocks are definitely lost in loss record 1 of 1
==15468==    at 0x4025BD3: malloc (vg_replace_malloc.c:236)
==15468==    by 0x8048601: newnode (myassignment.c:27)
==15468==    by 0x804885E: insertavl (myassignment.c:88)
==15468==    by 0x8048B33: main (myassignment.c:156)
==15468== 
==15468== LEAK SUMMARY:
==15468==    definitely lost: 20 bytes in 1 blocks
==15468==    indirectly lost: 0 bytes in 0 blocks
==15468==      possibly lost: 0 bytes in 0 blocks
==15468==    still reachable: 0 bytes in 0 blocks
==15468==         suppressed: 0 bytes in 0 blocks
==15468== 
==15468== For counts of detected and suppressed errors, rerun with: -v
==15468== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 13 from 8)
typedef struct node *tree;

struct node {
   int data;
   char name;
   int height;
   struct node *left;
   struct node *right;
};




static tree newnode(int d,char n){    
   tree t = malloc(sizeof(struct node));
   t->data = d;
   t->name=n;
   t->left = NULL;
   t->right = NULL;
   memoize(t);
   return t;
}

static tree modnode(int d, char n,tree t){
   if (d > t->data){
       t->data=d;
       t->name=n;
       t->right = t->right;}
   else{
       t->data=t->data;
       t->name=t->name;
       t->right = t->right;}
  return t;
}



tree insertavl(tree t, int d,const char *n){                 
   if (t == NULL) return newnode(d,*n);
   else if (t->name==*n) return modnode(d,*n,t->right);// <=========(1)
   else {                                                
      printf("went right");
      setright(t, insertavl(t->right, d,n));
   }
   return t;
}


int main(void) {
  int i = 0;
  char foo[11];
  int c;
  tree t= NULL;
  for(c=0;c<=1000;c++) {
    i = readstring(foo,10);
    if(i == 0) break;
    if (strcmp("score",foo)==0){
   int a=0;
   int point;
   char n[21];
   printf ("Enter name \n");
   a=readstring (n,20);
   printf ("Enter score \n");
   scanf("%d", &point);
   t=insertavl(t,point,n);}
   if (strcmp("highscore",foo)==0){
       printf ("%d \n", highsc(t));}

   if (t==NULL){
      printf ("false");}
    }
   return 0;
 }

我知道我的程序效率不高,但我想先修复错误。,感谢任何帮助!

4

2 回答 2

1

您突出显示的行:

else if (t->name==*n) return modnode(d,*n,t->right);// <=========(1)

想想当t一个新节点同时设置t->leftt->right设置为时会发生什么NULL

于 2012-07-25T08:22:46.717 回答
0

那么检查 malloc 的返回值呢?

于 2012-07-25T08:37:02.263 回答