Note that there are some explanatory texts on larger screens.

plurals
  1. POSegmentation fault in an attempt to implement a binary tree
    primarykey
    data
    text
    <p>I am trying to implement a binary tree structure in C++. I want a tree with '8' depth i.e. I want an attribute_struct with 1 attribute_node and 2 pointers to other 2 attribute_struct to make a binary tree. Here is the data structure and its creation method that I defined :</p> <pre><code>struct person{ public : char name[20]; }; struct attribute_node{ int attribute; person * child_person; }; struct attribute_struct{ attribute_node head_node; attribute_struct * yes_node; attribute_struct * no_node; }; attribute_struct initialize_struct(attribute_struct initial_struct, int prop){ if(prop &lt; 8){ attribute_struct new_yes_struct, new_no_struct; attribute_node temp_yes_node, temp_no_node; temp_yes_node.attribute = prop; temp_yes_node.child_person = NULL; temp_no_node.attribute = prop; temp_no_node.child_person = NULL; new_yes_struct.head_node = temp_yes_node; new_no_struct.head_node = temp_no_node; new_yes_struct.yes_node = NULL; new_no_struct.yes_node = NULL; new_yes_struct.no_node = NULL; new_no_struct.no_node = NULL; attribute_struct temp_yes_struct = initialize_struct(new_yes_struct, prop+1), temp_no_struct = initialize_struct(new_no_struct, prop+1); initial_struct.yes_node = &amp;temp_yes_struct; initial_struct.no_node = &amp;temp_no_struct; return initial_struct; }else{ person temp_person; strcpy(temp_person.name, "temp"); initial_struct.head_node.child_person = &amp;temp_person; return initial_struct; } } attribute_struct create_initial_attribute_structure(){ attribute_struct main_structure; attribute_node temp_node; temp_node.attribute = 3; temp_node.child_person = NULL; main_structure.head_node = temp_node; main_structure.yes_node = NULL; main_structure.no_node = NULL; main_structure = initialize_struct(main_structure, 1); return main_structure; } </code></pre> <p>Well, I am getting a segmentation fault when I try to use the structure as below.</p> <pre><code>int main(){ attribute_struct main_struct = create_initial_attribute_structure(); attribute_struct * current_head; current_head = &amp;main_struct; int iter = 0; while(iter &lt; 7){ current_head = (*current_head).yes_node; cout &lt;&lt; iter &lt;&lt; endl; iter++; } return 0; } </code></pre> <p>I cant determine whether the data structure is created wrong or if I am referencing it the wrong way. I used gdb but as its in the main function with no parameters I am not able to deduce anything.</p> <p>Well the seg fault is after printing iter 2. So, how can I determine whether the attibute_struct defined is correct and with 8 level of depth or whether I am referencing a NULL object? Using if(current_head == NULL) cout &lt;&lt; "Error"; inside the while loop does not help.</p>
    singulars
    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.
 

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