2013-03-21 59 views
-2

由於段錯誤,我無法編譯它。使用樹象限顯示最大容量會給我那個錯誤。奇怪的是它在函數象限內工作,但在插入點不起作用。創建樹功能很好,也是象限。但是當我嘗試訪問樹象限內的某個東西時(這個象限不是NULL,我之前檢查過)讓我運行一個segfault問題通知。恐怕這是一個非常簡單的錯誤,但我看不到它是什麼。我試圖在互聯網上搜索(並沒有發現任何東西),但我沒有時間來完成這個完整的程序(並且我堅持了幾個小時)。 任何人都可以幫助我嗎? 下面的代碼:C - 一個奇怪的Seg故障錯誤

#include <stdlib.h> 
    #include <stdio.h> 
    #include <float.h> 
    #include <limits.h> 



     typedef struct dot{ 
      double x; 
      double y; 
     }Dot; 


     typedef struct quadrant{ 
      int max_capacity, used_capacity; 
      Dot max,min; 
      Dot * dots_; 
     }Quadrant; 


     typedef struct quad_node * Quad_node_Pointer; 

     typedef struct quad_node{ 
      Quadrant * key; 
      Quad_node_Pointer child[4]; 
      Quad_node_Pointer father; 
     }Quad_node; 


     typedef struct tree{ 
      Quad_node * end_; 
      Quad_node * start_; 
     }Tree; 





     void insert_dot(Tree * A, Dot b){ 
      printf("lalala\n"); 
      Quad_node * Aux, *Aux2, * New_leafs[4]; 
      Dot min_aux,max_aux; 
      int i; 
      Aux=(Quad_node_Pointer) malloc (sizeof(Quad_node)); 
      Aux2=(Quad_node_Pointer) malloc (sizeof(Quad_node)); 
      printf("lalala\n"); 
      //Here's the segfault line: 
      printf("this doesnt works %i",A->start_->key->max_capacity); 

     void Create_quadrant (Quadrant * A, int capacity, Dot max, Dot min){ 
      A=(Quadrant*)malloc(sizeof(Quadrant)); 
      A->dots_ = (Dot*) malloc (capacity * sizeof(Dot)); 
      int i; 
      for (i=0;i<capacity;i++){ 
       A->dots_[i].x=-1; 
       A->dots_[i].y=-1; 
      } 
      A->max_capacity=capacity; 
      //But here it works perfectly. What's the diference from the other that do 
      //a segfault? 
      printf("\n this works \n %i \n",A->max_capacity); 
      A->used_capacity=0; 
      A->max.x=max.x; 
      A->max.y=max.y; 
      A->min.y=min.y; 
      A->min.x=min.x; 
      } 

    void Create_tree (Tree * A, int capacity){ 
     int i; 
     Dot max,min; 
     max.x=DBL_MAX; 
     max.y=DBL_MAX; 
     min.x=0; 
     min.y=0; 
     A->end_ = (Quad_node_Pointer) malloc (sizeof(Quad_node)); 
     A->start_=(Quad_node_Pointer) malloc (sizeof(Quad_node)); 
      for (i=0;i<4;i++){ 
       A->start_->child[i]=A->end_; 
      } 
      A->start_->father=A->end_; 

     Create_quadrant(A->start_->key,capacity,max,min); 
    } 

這裏的主,只是一個例子:

int main(int argc, char *argv[]) 
{ 
    Tree * A; 
    int i; 
    A = (Tree*) malloc (sizeof(Tree)); 
    Dot b,teste[10]; 
    b.x=5.0; 
    b.y=6.0; 
    Create_tree(A,8); 
    for (i=0;i<10;i++){ 
     teste[i].x=(double)2.0*i; 
     teste[i].y=(double)2.0*i; 
     insert_dot(A,teste[i]); 
    } 
    insert_dot(A,b); 
    free(A); 
    return EXIT_SUCCESS; 
} 

感謝您的閱讀和/或幫助我。編輯: 只是爲了記住,我忘記了。插入點函數在那裏沒有滿。重點在於段故障問題。主要來自基於全功能運行的示例。對不起,有任何麻煩。但現在我的問題是這個奇怪的段錯誤。我認爲該函數的其餘部分是可以的,我省略了讓我的問題更簡單(並且與函數的其餘部分無關)。

+1

段錯誤發生在運行時,而不是編譯時。嘗試使用GDB進行調試或運行Valgrind。 – 2013-03-21 02:48:01

+0

快速查看你的'Create_tree(A,8)'看起來很低,因爲你迭代了10個元素。試試'Create_tree(A,10)' – dchhetri 2013-03-21 02:48:31

+0

我已經調試過了。錯誤是: //這是段錯誤行: printf(「這沒有工作%i」,A->開始_->鍵 - > max_capacity); 但我不知道爲什麼它無法訪問或無法識別樹象限中的max_capacity。 – 2013-03-21 02:54:13

回答

2

我們走吧......我將展示相關的代碼,忽略其中不相關的行。

首先,分配存儲空間和初始化樹...

A = (Tree*) malloc (sizeof(Tree)); 
Create_tree(A,8); 

Create_tree函數初始化上A東西:

A->end_ = (Quad_node_Pointer) malloc (sizeof(Quad_node)); 
    A->start_=(Quad_node_Pointer) malloc (sizeof(Quad_node)); 
    for (i=0;i<4;i++){ 
     A->start_->child[i]=A->end_; 
    } 
    A->start_->father=A->end_; 

好了,現在A->start_A->end_有未初始化的存儲,除了你已經在A->start_->child[]中設置了四個子指針。

此時,您致電Create_quadrant初始化A->start_->key,傳遞一個未初始化的指針。

Create_quadrant(A->start_->key,capacity,max,min); 

下面是函數聲明:

void Create_quadrant (Quadrant * A, int capacity, Dot max, Dot min); 

有沒有辦法讓你的新初始化象限取消入A->start_->key。顯然,你想這樣做,因爲該函數的第一行做到這一點:

 A=(Quadrant*)malloc(sizeof(Quadrant)); 

這打破你的代碼的模式,到目前爲止,在那裏你承擔責任的分配數據,然後調用一個函數初始化它。如果你想讓init函數返回一個在函數內部分配的指針,你需要返回它或者傳遞一個雙指針。

所以選項1:

Quadrant * Create_quadrant (int capacity, Dot max, Dot min) 
{ 
    A=(Quadrant*)malloc(sizeof(Quadrant)); 
    //... 
    return A; 
} 

// Called like this: 
A->start_->key = Create_quadrant(capacity, max, min); 

和Option 2:

void Create_quadrant (Quadrant ** pA, int capacity, Dot max, Dot min) 
{ 
    A=(Quadrant*)malloc(sizeof(Quadrant)); 
    // ... 
    *pA = A;   
} 

// Called like this: 
Create_quadrant(&A->start_->key, capacity, max, min); 

我忘了提,選擇0繼續,因此到目前爲止,你已經使用的慣例:

// Called like this: 
A->start_->key = (Quadrant*)malloc(sizeof(Quadrant)); 
Create_quadrant(A->start_->key, capacity, max, min); 

// And obviously you DON'T malloc a new A inside Create_quadrant(). 
+0

謝謝。現在完美運作。對不起,我不能投票,因爲它需要15點聲望,但是當我得到它時,我會記得去做。 再次,非常感謝。 – 2013-03-21 17:41:21

0

我的猜測是,你不malloc荷蘭國際集團足夠的空間,爲您Quadrant S,因爲你只給他們儘可能多的房間內存爲Quad_node會佔用了,想必Quad_node。就拿起來比Quadrant更少的空間秒。