Note that there are some explanatory texts on larger screens.

plurals
  1. POC random tree pointer warning (and sometimes crashing)
    primarykey
    data
    text
    <p>I'm writing a program to create a trinary tree of random size. That is, I add 0-3 branches for each node randomly, then see how large the tree is. The goal is to run this a few thousand times and then do statistics -- size, diameter, depth, etc.</p> <p>I use a basic tree structure, and then a pointer of pointers to be able to order the nodes (0,1,2,3) based on when they were created, called tab_of_order.</p> <p>The current code, however, crashes randomly (maybe 1 time out of 5). At the bottom I give an example of what a crash looks like. The ArbreAlea() seems to run fine, but crashes on the return. I can't see why, either. Until it stops crashing I can't conceivably do a loop for 1000 cases.</p> <p>On another note, at the beginning of the function to create the random tree, I get a pointer warning "warning: assignment from incompatible pointer type [enabled by default]". Would this lead to my function crashing? Any idea how to fix it?</p> <p>The two functions I didn't paste are just functions to create a random number (0,1,2,3,4) with changing probabilities.</p> <p>Any help greatly appreciated!</p> <pre><code>#include&lt;stdlib.h&gt; #include&lt;stdio.h&gt; // 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-&gt;nb_vertices); return 0; } void add_node(int nb_node_to_add, myNode **tab_of_order, struct queue *arbreQ){ int i; int next = arbreQ-&gt;debut; for (i=1; i &lt;= nb_node_to_add; i++){ myNode *tmp_node; tmp_node = (myNode*) malloc(sizeof(myNode)*1); tmp_node-&gt;position = arbreQ-&gt;fin + i; if(i==1){ tab_of_order[next]-&gt;first = tmp_node; } else if(i==2) { tab_of_order[next]-&gt;second = tmp_node; } else if(i==3) { tab_of_order[next]-&gt;third = tmp_node; } else if(i==4) { tab_of_order[next]-&gt;fourth = tmp_node; } tab_of_order[tmp_node-&gt;position] = tmp_node; //printf("Tab_of_order[ %d ] %d connected to node %d\n", next , i, tab_of_order[tmp_node-&gt;position]-&gt;position); } //printf("add to node %d, %d node(s) is/are added\n", next , nb_node_to_add); arbreQ-&gt;fin = arbreQ-&gt;fin + nb_node_to_add; arbreQ-&gt;debut +=1; tab_of_order[arbreQ-&gt;fin + 1] = NULL; //printf("\nDebut %d Fin %d\n",arbreQ-&gt;debut,arbreQ-&gt;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-&gt;debut=0; arbreQ-&gt;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-&gt;position = 0; tab_of_order[0] = tree; //next = 0; nb_node_to_add = rand_loi1(arbreQ-&gt;debut); if (nb_node_to_add == 0){ data-&gt;nb_vertices=1; data-&gt;nb_feuilles=0; data-&gt;profondeur=0; data-&gt;largeur=0; data-&gt;diametre=0; printf("\nZero Tree"); return(data); } else { do { nb_node_to_add=rand_loi1(arbreQ-&gt;debut); printf("\nAdding %d nodes, debut %d, fin %d",nb_node_to_add,arbreQ-&gt;debut,arbreQ-&gt;fin); if (nb_node_to_add==0) arbreQ-&gt;debut += 1; else if (nb_node_to_add&gt;=1) add_node(nb_node_to_add, tab_of_order, arbreQ); nb_node = nb_node + nb_node_to_add; if (tab_of_order[arbreQ-&gt;debut]==NULL) printf("\n Next is null : %d",arbreQ-&gt;debut); }while (tab_of_order[arbreQ-&gt;debut]!=NULL); } data-&gt;nb_vertices=nb_node+1; data-&gt;profondeur=0; data-&gt;largeur=0; printf("\nTree debut %d fin %d nodes %d",arbreQ-&gt;debut,arbreQ-&gt;fin,nb_node+1); return (data); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload