这是我的代码的一部分...而且我遇到了几个主要问题...
好的,到目前为止,我能看到的大多数问题都已解决。
当我结束程序时,这是错误消息: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;
}
我知道我的程序效率不高,但我想先修复错误。,我们将不胜感激!
最佳答案
那么检查malloc的返回值呢?
关于c - 访问NULL指针,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11645371/