title: C program return 0xc0000005 status
body: <p>I'm trying to code a C program that convert a binary tree into a linket list. Here's the declaration of these to structures :</p>
  
  <pre><code>struct treeNode {
      double data;
      struct treeNode* leftP;
      struct treeNode* rightP;
  };
  
  struct listNode{
      double data;
      struct listNode* nextP;
  };
  </code></pre>
  
  <p>The algorithm works but I have waht I expect to be a memory error at the end of the program's execution (process returned 0xC0000005). After some search with this code, I guess it comes from a bad management of pointers but I can't find my mistake. Here's my main function :</p>
  
  <pre><code>int main()
  {
      /*Creation of the binary tree*/
      struct treeNode tree[10];
      int i;
      for(i=0;i&lt;10;i++)
          tree[i].data = (float)i;
      tree[0].leftP = &amp;(tree[1]);
      tree[0].rightP = &amp;(tree[2]);
      //....... (creation of the tree)
      tree[9].rightP = 0;
  
      //Conversion
      void** elem = malloc(sizeof(void**));
      *elem = (void*)(&amp;tree);
      tree2list(elem);
      struct listNode* list = (struct listNode*)(*elem);
      printList(list);
      printf("end1");
      //free(elem);
      printf("end2\n");
      return 0;
  }
  </code></pre>
  
  <p>The program reach end2 if I comment the free line and end1 if I try to free the variable elem. Here's the tree2list function :</p>
  
  <pre><code>void tree2list(void** node)
  {
      struct treeNode* tree = (struct treeNode*)(*node); //tree to convert
      int size = treeSize(tree);
      struct listNode* list = malloc(size*sizeof(struct listNode*)); //Creation of a list with same number of nodes than in the tree
      *node = (void*) list;
      struct listNode* currentListNode = list;
      struct treeNode* currentTreeNode = tree;
      struct treeNode* nextNode;
      struct listNode* old = 0;
      struct treeNode* stack = 0; //Stack composed of treeNode linked by their leftP pointer
      while(currentTreeNode)
      {
          if(currentTreeNode-&gt;leftP) //if left branch exists, add the current node to the stack and explore this left branch
          {
              nextNode = currentTreeNode-&gt;leftP;
              stackPush(&amp;stack, currentTreeNode);
              currentTreeNode = nextNode;
          }
          else //if left branch doesn't exist, add the currentNode to the list
          {
              currentListNode-&gt;data = currentTreeNode-&gt;data;
              currentListNode-&gt;nextP = 0;
              if(old)
                  old-&gt;nextP = currentListNode;
              old = currentListNode++;
              if(currentTreeNode-&gt;rightP)
                  currentTreeNode = currentTreeNode-&gt;rightP;
              else
                  currentTreeNode = stackPop(&amp;stack);
          }
      }
  }
  </code></pre>
  
  <p>This function of other function like stackPush and stackPop that seem to work well. Do someone see the source of error code ?</p>
  
  <p>Thanks</p>