C 随机树指针警告(有时会崩溃)

标签 c pointers struct tree crash

我正在编写一个程序来创建随机大小的三叉树。也就是说,我为每个节点随机添加0-3个分支,然后看看树有多大。目标是运行几千次,然后进行统计——尺寸、直径、深度等。

我使用基本的树结构,然后使用一个指针指针,以便能够根据节点的创建时间对节点 (0,1,2,3) 进行排序,称为 tab_of_order。

然而,当前的代码会随机崩溃(可能五次中就有一次)。在底部我给出了一个崩溃的例子。 ArbreAlea() 似乎运行良好,但返回时崩溃。我也不明白为什么。在它停止崩溃之前,我无法想象循环执行 1000 个案例。

另一方面,在创建随机树的函数开始时,我收到一个指针警告“警告:来自不兼容指针类型的赋值[默认启用]”。这会导致我的功能崩溃吗?知道如何修复它吗?

我没有粘贴的两个函数只是创建概率变化的随机数 (0,1,2,3,4) 的函数。

非常感谢任何帮助!

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

// Structure of the tree. Position is the value of the node. The nodes are 
// numbered 1 to n thus the number gives it's position 
struct tree_el {
    int position;
    struct tree_el * first, * second, * third, * fourth;
};
typedef struct tree_el myNode;

//Structure of the data to be analysed
struct donnees {
 int nb_vertices;
 int nb_feuilles;
 int profondeur;
 int largeur;
 int diametre;
};

//Simplified queue structure
struct queue {
int debut;
int fin;
};

//initialize functions
int get_rnd_value (int min, int max);
int rand_loi1 (int iteration);
void add_node (int nb_node_to_add, myNode **tab_of_order, struct queue *arbreQ);
struct donnees *  ArbreAlea (void);

int main(void)
{
    srand((unsigned)time(NULL)); //seed random number generator
int nb_node=0;
struct donnees *resultat;
resultat=(struct donnees*)malloc(sizeof(struct donnees)*1);
    resultat = ArbreAlea();
    printf("\n\nNombre Vertices : %d \n",resultat->nb_vertices);

    return 0;
}

void add_node(int nb_node_to_add, myNode **tab_of_order, struct queue *arbreQ){
    int i;
    int next = arbreQ->debut;

    for (i=1; i <= nb_node_to_add; i++){
        myNode *tmp_node;
        tmp_node = (myNode*) malloc(sizeof(myNode)*1);
        tmp_node->position = arbreQ->fin + i;
        if(i==1){
            tab_of_order[next]->first = tmp_node;
        }
        else if(i==2) {
            tab_of_order[next]->second = tmp_node;
        }
        else if(i==3) {
            tab_of_order[next]->third = tmp_node;
        }
        else if(i==4) {
            tab_of_order[next]->fourth = tmp_node;
        }
        tab_of_order[tmp_node->position] = tmp_node;
        //printf("Tab_of_order[ %d ] %d connected to node %d\n", next , i, tab_of_order[tmp_node->position]->position);
    }
    //printf("add to node %d, %d node(s) is/are added\n", next , nb_node_to_add);

    arbreQ->fin = arbreQ->fin + nb_node_to_add;
    arbreQ->debut +=1;
    tab_of_order[arbreQ->fin + 1] = NULL;
    //printf("\nDebut %d Fin %d\n",arbreQ->debut,arbreQ->fin);

    return;
}

struct donnees * ArbreAlea (void) {
    struct donnees *data;
    //Here i get "warning: assignment from incompatible pointer type [enabled by default]"
    data = (struct donnnes *)malloc(sizeof(struct donnees)*1);
    struct queue *arbreQ;
    arbreQ = (struct queue *)malloc(sizeof(struct queue)*1);
    arbreQ->debut=0;
    arbreQ->fin=0;

    int nb_node = 0;  //nb_node is the number total of nodes in tree.
    int nb_node_to_add;  // nb_node_to_add is how many nodes you have to add for each node.

// this table keeps track of the order of nodes to quickly look up where to add the next node. 
    myNode ** tab_of_order;
    tab_of_order = (myNode **) malloc(sizeof(myNode*) * nb_node);

// First we make the begninning of the tree. This is the only time it can have 4 branches.
// There is a 1/4 chance of having a tree of size one.
    myNode *tree;
    tree = (myNode*) malloc(sizeof(myNode) * 1 ) ;
    tree->position = 0;
    tab_of_order[0] = tree;
    //next =  0;
    nb_node_to_add = rand_loi1(arbreQ->debut);

    if (nb_node_to_add == 0){
        data->nb_vertices=1;
        data->nb_feuilles=0;
        data->profondeur=0;
        data->largeur=0;
        data->diametre=0;
        printf("\nZero Tree");
        return(data);
    }
    else {      
        do {
            nb_node_to_add=rand_loi1(arbreQ->debut);
            printf("\nAdding %d nodes, debut %d, fin %d",nb_node_to_add,arbreQ->debut,arbreQ->fin);
            if (nb_node_to_add==0)
                arbreQ->debut += 1;
            else if (nb_node_to_add>=1)
                add_node(nb_node_to_add, tab_of_order, arbreQ);
            nb_node = nb_node + nb_node_to_add;
            if (tab_of_order[arbreQ->debut]==NULL)
                printf("\n Next is null : %d",arbreQ->debut);
        }while (tab_of_order[arbreQ->debut]!=NULL);                     
    }
    data->nb_vertices=nb_node+1;
    data->profondeur=0;
    data->largeur=0;
    printf("\nTree debut %d fin %d nodes %d",arbreQ->debut,arbreQ->fin,nb_node+1);
    return (data);
}

最佳答案

问题是索引越界!

关于C 随机树指针警告(有时会崩溃),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16383674/

相关文章:

c - 为单元测试构建过程代码

c++ - C++中的非空终止字符数组

c - void * 指向列表中的 void *

c++ - 读入动态创建的指向结构数组的指针

c - scanf 之后 fgets 不起作用

c - 我不明白 malloc 函数

c++ - 理解 C++ 中的 "Bitwise-And (&)"和 "Unary complement(~)"

c - 为什么返回指向局部变量或参数的指针是不好的做法?

c - 将结构作为参数传递给 pthread

C++将数据从数据文件输入到结构变量中