C随机树指针警告(有时崩溃)
我正在写一个程序来创建一个随机大小的三叉树。也就是说,我为每个节点随机添加0-3个分支,然后查看树有多大。目标是运行数千次,然后进行统计——大小、直径、深度等 我使用了一个基本的树结构,然后是一个指针指针,能够根据节点(0,1,2,3)的创建时间对它们进行排序,称为tab\u of_order 但是,当前代码会随机崩溃(可能是5次中的1次)。在底部,我给出了一个崩溃的例子。arbrearea()似乎运行良好,但返回时崩溃。我也不明白为什么。在它停止崩溃之前,我不可能为1000个箱子做一个循环 另一方面,在创建随机树的函数开始时,我得到一个指针警告“警告:来自不兼容指针类型的赋值[默认启用]”。这会导致我的函数崩溃吗?知道怎么修吗 我没有粘贴的两个函数只是创建概率变化的随机数(0,1,2,3,4)的函数 非常感谢您的帮助C随机树指针警告(有时崩溃),c,pointers,struct,tree,crash,C,Pointers,Struct,Tree,Crash,我正在写一个程序来创建一个随机大小的三叉树。也就是说,我为每个节点随机添加0-3个分支,然后查看树有多大。目标是运行数千次,然后进行统计——大小、直径、深度等 我使用了一个基本的树结构,然后是一个指针指针,能够根据节点(0,1,2,3)的创建时间对它们进行排序,称为tab\u of_order 但是,当前代码会随机崩溃(可能是5次中的1次)。在底部,我给出了一个崩溃的例子。arbrearea()似乎运行良好,但返回时崩溃。我也不明白为什么。在它停止崩溃之前,我不可能为1000个箱子做一个循环 另
#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);
}
#包括
#包括
//树的结构。位置是节点的值。节点是
//编号为1到n,因此该数字给出了它的位置
结构树{
内部位置;
结构树第一,*第二,*第三,*第四;
};
typedef结构树_elmynode;
//待分析数据的结构
结构唐尼{
int nb_顶点;
内恩布费尔斯;
内特教授;
国际大酒店;
内径;
};
//简化队列结构
结构队列{
int首次亮相;
内鳍;
};
//初始化函数
int get_rnd_值(int min,int max);
int rand_loi1(int迭代);
void add_node(int nb_node_to_add,myNode**tab_of_order,struct queue*arbreQ);
结构donnees*Arbrearea(无效);
内部主(空)
{
srand((无符号)时间(NULL));//种子随机数生成器
int nb_节点=0;
结构donnees*resultat;
结果=(结构donnees*)malloc(大小f(结构donnees)*1);
resultat=arbreaea();
printf(“\n\n不规则顶点:%d\n”,结果->不规则顶点);
返回0;
}
void add_node(int nb_node_to_add,myNode**tab_of_order,struct queue*arbreQ){
int i;
int next=arbreQ->首次登场;
对于(i=1;i位置=arbreQ->fin+i;
如果(i==1){
_顺序的tab_[next]->first=tmp_节点;
}
else如果(i==2){
_顺序的tab_[next]->second=tmp_节点;
}
else如果(i==3){
_顺序的tab_[next]->third=tmp_节点;
}
else如果(i==4){
_顺序的tab_[next]->fourth=tmp_节点;
}
订单页签【tmp\U节点->位置】=tmp\U节点;
//printf(“订单[%d]%d的Tab\u连接到节点%d\n”,接下来是i,订单的Tab\u[tmp\u节点->位置]->position];
}
//printf(“添加到节点%d,添加了%d个节点”,下一步,添加到节点);
arbreQ->fin=arbreQ->fin+nb\u节点\u添加;
arbreQ->首次登场+=1;
订单的制表符[arbreQ->fin+1]=空;
//printf(“\n但%d Fin%d\n”,arbreQ->首次亮相,arbreQ->Fin);
返回;
}
结构donnees*ArbreAlea(无效){
结构donnees*数据;
//这里我得到了“警告:来自不兼容指针类型的分配[默认启用]”
数据=(struct donnees*)malloc(sizeof(struct donnees)*1);
结构队列*arbreQ;
arbreQ=(结构队列*)malloc(sizeof(结构队列)*1);
arbreQ->0;
arbreQ->fin=0;
int nb_node=0;//nb_node是树中的节点总数。
int nb_node_to_add;//nb_node_to_add是每个节点必须添加的节点数。
//此表跟踪节点的顺序,以便快速查找下一个节点的添加位置。
订单的myNode**页签;
tab_of_order=(myNode**)malloc(sizeof(myNode*)*nb_node);
//首先,我们开始做这棵树。这是它唯一一次可以有4根树枝。
//有1/4的几率拥有一棵大小的树。
myNode*树;
tree=(myNode*)malloc(sizeof(myNode)*1);
树->位置=0;
订单[0]的tab\u=树;
//next=0;
nb_node_to_add=rand_loi1(arbreQ->首次登场);
如果(nb_节点到_添加==0){
数据->nb_顶点=1;
数据->nb_feuilles=0;
数据->profondeur=0;
data->largeur=0;
数据->直径=0;
printf(“\nZero树”);
返回(数据);
}
否则{
做{
nb_node_to_add=rand_loi1(arbreQ->首次登场);
printf(“\n添加%d个节点,首次登场%d,财务%d”,nb\u节点添加,arbreQ->dedunch,arbreQ->fin);
如果(nb_节点到_添加==0)
arbreQ->首次登场+=1;
否则如果(nb_节点到_添加>=1)
添加_节点(nb_节点_至_添加,订单选项卡_,arbreQ);
nb_node=nb_node+nb_node_to_add;
if(订单的制表符[arbreQ->首次登场]==NULL)
printf(“\n下一个为空:%d”,arbreQ->首次登场);
}while(订单的制表符[arbreQ->处女秀]!=NULL);
}
数据->nb_顶点=nb_节点+1;
数据->profondeur=0;
data->largeur=0;
printf(“\n查看首次登场%d个fin%d个节点%d”,arbreQ->首次登场,arbreQ->fin,nb_节点+1);
返回(数据);
}
问题是一个越界索引!您应该在这里的代码中使用英文注释和看起来像英文的标识符。您是否尝试使用所有警告(即Linux上的gcc-Wall-g
)进行编译,并使用调试器(即Linux上的gdb
)?并改进您的代码,直到您没有收到警告!所有的注释都是英文的,但是变量有时是用法语命名的。很抱歉。警告是由于struct donnes
中缺少e
,我不相信这是导致警告的原因,因为没有名为donnes的元素,如果我使用它,将有一个error而不是警告。