gpt4 book ai didi

C - 一个奇怪的 Seg 错误

转载 作者:行者123 更新时间:2023-11-30 20:15:54 25 4
gpt4 key购买 nike

由于段错误,我无法编译它。使用树象限显示最大容量给了我这个错误。奇怪的是,它在函数 Quadrant 中工作,但在插入 Dot 中不起作用。创建树功能很好,象限也很好。但是,当我尝试访问树象限内的某些内容(象限不是 NULL,我之前检查过)时,我会继续运行段错误问题通知。恐怕这是一个非常简单的错误,但我看不出它是什么。我尝试在互联网上搜索(但一无所获),但我已经没有时间完成这个完整的程序了(而且我在这个程序上停留了几个小时)。有人可以帮我吗?代码如下:

    #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;
}

感谢您阅读或/和帮助我。

编辑:只是为了记住,我已经忘记了。那里的插入点功能并不完整。重点是段错误问题。主要来自它基于完整功能运行的示例。抱歉造成任何麻烦。但我现在的问题是这个奇怪的段错误。我认为函数的其余部分没问题,并且我省略了让我的问题更简单(并且与函数的其余部分无关)。

最佳答案

开始吧...我将显示相关代码,忽略之间不相关的行。

首先,分配存储空间并初始化树...

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 );

选项 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().

关于C - 一个奇怪的 Seg 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15538556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com