Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 访问空指针_C_Gcc - Fatal编程技术网

C 访问空指针

C 访问空指针,c,gcc,C,Gcc,这是我代码的一部分。。。我有几个主要的问题,我坚持。。。 好的,到目前为止我看到的大部分问题都已经解决了。。。但是我仍然不知道该在哪里释放记忆。 这是结束程序时的错误消息: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 byte

这是我代码的一部分。。。我有几个主要的问题,我坚持。。。 好的,到目前为止我看到的大部分问题都已经解决了。。。但是我仍然不知道该在哪里释放记忆。 这是结束程序时的错误消息: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;
 }
堆摘要:
==15468==在出口处使用:1个块中有20个字节
==15468==总堆使用率:1个allocs,0个frees,分配20个字节
==15468== 
==15468==1个块中的20个字节在丢失记录1(共1个)中肯定丢失
==15468==0x4025BD3:malloc(vg\u replace\u malloc.c:236)
==15468==by 0x8048601:newnode(myassignment.c:27)
==15468==by0x804885e:insertavl(myassignment.c:88)
==15468==0x8048B33:main(myassignment.c:156)
==15468== 
==15468==泄漏汇总:
==15468==肯定丢失:1个块中有20个字节
==15468==间接丢失:0个块中有0个字节
==15468==可能丢失:0个块中有0个字节
==15468==仍然可访问:0个块中有0个字节
==15468==抑制:0个块中有0个字节
==15468== 
==15468==对于检测到的和抑制的错误计数,请使用:-v重新运行
==15468==错误摘要:1个上下文中的1个错误(已抑制:8个中的13个)
typedef结构节点*树;
结构节点{
int数据;
字符名;
内部高度;
结构节点*左;
结构节点*右;
};
静态树newnode(intd,charn){
树t=malloc(sizeof(struct node));
t->data=d;
t->name=n;
t->left=NULL;
t->right=NULL;
记忆化(t);
返回t;
}
静态树modnode(int d,char n,tree t){
如果(d>t->数据){
t->data=d;
t->name=n;
t->right=t->right;}
否则{
t->data=t->data;
t->name=t->name;
t->right=t->right;}
返回t;
}
树插入符(树t,int d,const char*n){
如果(t==NULL)返回newnode(d,*n);
如果(t->name==*n)返回modnode(d,*n,t->right);//right,d,n));
}
返回t;
}
内部主(空){
int i=0;
char-foo[11];
INTC;
树t=NULL;

对于(c=0;c突出显示的行:

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

else如果(t->name==*n)返回modnode(d,*n,t->right);//左
和右
设置为
NULL

检查malloc的返回值怎么样?

尝试将其减少为一个。在第37行,您取消了对NULL指针的引用;关于这一点,没什么要说的了。顺便说一下,您的名字只有一个字符吗?!我看到一个
malloc
但没有相应的
free()
。在
modnode
中,为什么分配
t->right=t->right;
?它什么都不做。啊……哎呀。这是一个问题解决了!非常感谢!还有一件事……你知道我应该在哪里释放malloc吗?@user1516649在你的程序中,你永远不会删除节点,但那将是一个释放分配给它们的内存的好地方.问题是我不需要删除节点..我只需要修改或添加新节点.这就是为什么我对在哪里释放感到困惑的原因it@user1516649那么你不必释放它,当程序结束时,系统会为你释放它。?很抱歉,我不明白…为什么我要检查malloc?静态树newnode的返回值(int d,char n){tree t=malloc(sizeof(struct node));if(!t)anerrorapper;else{t->data=d;t->name=n;t->left=NULL;t->right=NULL;memoize(t);return t;}return NULL;}