由於段錯誤,我無法編譯它。使用樹象限顯示最大容量會給我那個錯誤。奇怪的是它在函數象限內工作,但在插入點不起作用。創建樹功能很好,也是象限。但是當我嘗試訪問樹象限內的某個東西時(這個象限不是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;
}
感謝您的閱讀和/或幫助我。編輯: 只是爲了記住,我忘記了。插入點函數在那裏沒有滿。重點在於段故障問題。主要來自基於全功能運行的示例。對不起,有任何麻煩。但現在我的問題是這個奇怪的段錯誤。我認爲該函數的其餘部分是可以的,我省略了讓我的問題更簡單(並且與函數的其餘部分無關)。
段錯誤發生在運行時,而不是編譯時。嘗試使用GDB進行調試或運行Valgrind。 – 2013-03-21 02:48:01
快速查看你的'Create_tree(A,8)'看起來很低,因爲你迭代了10個元素。試試'Create_tree(A,10)' – dchhetri 2013-03-21 02:48:31
我已經調試過了。錯誤是: //這是段錯誤行: printf(「這沒有工作%i」,A->開始_->鍵 - > max_capacity); 但我不知道爲什麼它無法訪問或無法識別樹象限中的max_capacity。 – 2013-03-21 02:54:13