C - Null pointer issue on AVL tree rotation implementation -


i'm implementing avl tree in c. i've posted tree rotations below, valgrind errors when try test them.

why getting these errors? understand valgrind errors stem fact i'm using null pointers, can't pinpoint i'm doing wrong. (i've commented on lines of valgrind errors)

tree rotateright(tree t) {     tree temp = t->l;     t->l=temp->r;     temp->r=t;     temp->height=maximum(heightt(temp->l), heightt(temp->r));     t->height=maximum(heightt(t->l), heightt(t->r));     return t; }  tree rotateleft(tree t) {     tree temp = t->r; //this line 226     t->r=temp->l;     temp->l=t;     temp->height=maximum(heightt(temp->l), heightt(temp->r));     t->height=maximum(heightt(t->l), heightt(t->r));     return t; }   tree rotateleftright(tree t) {     t->l=rotateleft(t->l); //line 235     t=rotateright(t);     return t; }  tree rotaterightleft(tree t) {     t->r=rotateright(t->r);     t=rotateleft(t);     return t; } 

valgrind errors(i'm getting same thing rotateleft):

==20073== invalid read of size 8 ==20073==    @ 0x40196f: rotateleft (bst.c:226) ==20073==    0x401a11: rotateleftright (bst.c:235) ==20073==    0x4013a9: insertt (bst.c:69) ==20073==    0x400e77: addin (spell13.c:96) ==20073==    0x400cbe: main (spell13.c:59) ==20073==  address 0x10 not stack'd, malloc'd or (recently) free'd ==20073==  ==20073==  ==20073== process terminating default action of signal 11 (sigsegv) 

taking code have + error report, appears though tree this:

typedef struct tree_s {     struct tree_s *l;     struct tree_s *r; } tree; 

it appear tree->l passed rotateleftright null


Comments