title: Freeing a tree in C
body: <p>So I've been given the structure of a tree where each node has up to 2 children:</p>
  
  <pre><code>typedef struct one_node_t one_node_t;
  
  typedef struct two_nodes_t two_nodes_t;
  
  typedef struct my_tree_t {
    int numberOfNodes; //== 1 || numberOfNodes == 2
  
     // If the node is a leaf, it is represented by (*oneNode==NULL &amp;&amp; numberOfNodes == 1)
      union {
         one_node_t* one;
         two_nodes_t* two;
      } nodeC;
  } my_tree_t;
  
  struct one_node_t {
    one_node_t* child;
  };
  
  struct two_node_t {
    my_tree_t children[2];
  };
  </code></pre>
  
  <p>Now I was trying to write a function <code>void freeMyTree(my_tree_t* myTree)</code> which should free the allocated space of the tree <code>myTree</code> is pointing towards:</p>
  
  <pre><code>void freeMyTree(my_tree_t* myTree) {
    if(myTree-&gt;numberOfNodes == 2){
        freeMyTree(myTree-&gt;nodeC.two-&gt;children);
        freeMyTree(&amp;myTree-&gt;nodeC.two-&gt;children[1]);
        free(myTree-&gt;nodeC.two);
        myTree-&gt;nodeC.two = NULL;
    }else{
        if(myTree-&gt;nodeC.one-&gt;child != NULL)
            freeMyTree(myTree-&gt;nodeC.one-&gt;child);
        free(myTree-&gt;nodeC.one);
        myTree-&gt;nodeC.one = NULL;
    }
  }
  </code></pre>
  
  <p>But it seems like no space is freed. I guess there has to be some kind of misunderstanding of myself how to free up space, but googling didn't help me to understand it. </p>
  
  <p>I think that space is not freed up because I create my files using gcc with the <code>-fsanitize=leak</code> option.</p>
  
  <p>If I created a <code>my_tree_t* a = calloc(1, sizeof(my_tree_t))</code> earlier the <code>leak sanitizer</code> tells me if I did not free it. However after calling <code>freeMyTree(a)</code> it will still tell me the same thing.</p>
  
  <p><strong>Edit:</strong>
  The error messages explcitly says that a one/two was not freed. Because they are point to lines like:</p>
  
  <pre><code>my_tree_t t;
  t.nodeC.two = calloc(1, sizeof(two_nodes_t)); //&lt;== POINTING TO THIS LINE
  </code></pre>