2014-09-26 35 views
-2

我在這裏得到了seg故障。我困惑。 請幫我一把。 f1和y都是結構節點的指針。 我想將y左轉到f1右。請檢查我的segFault

#include <stdio.h> 
#include <stdlib.h> 
struct node{ 
      int data; 
       struct node* left; 
       struct node* right; 
}; 
int main(){ 
    struct node* f1=(struct node *)malloc(sizeof(struct node)); 
    struct node* y=(struct node *)malloc(sizeof(struct node)); 
    f1->data=10; 
    y=f1->right; 
    f1->right=y->left; //seg fault is in this line. 

    return 0; 
}` 
+0

另請參見[關於在C中轉換malloc結果](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Ilya 2014-09-26 22:56:26

+0

請勿投射malloc:http: //fackoverflow.com/a/605858/19410 – 2014-09-26 22:56:31

+1

'f1-> right'沒有初始化。和'y'被覆蓋(內存泄漏)。 – BLUEPIXY 2014-09-26 22:56:41

回答

2

y=f1->righty到未初始化的內存。 y->left現在無效。嘗試與Valgrind一起運行。