title: Memory allocation mistake on C
body: <p>I am trying to split set of numbers to 2 different heaps, from their middle value. I used heap data structure to do this. i.e Input is 10 in this example. Apparently I am making some mistake while allocating memory, it gives the following output when I try to allocate dynamically:</p>
  <pre><code>[ 4 3 2 0 1 ]
  
  [ 0 0 0 0 0 ]
  </code></pre>
  <p>why the second heap is always 0 ?</p>
  <p>Because if I uncomment the static memory allocation and use it instead, it gives true output which is:</p>
  <pre><code>[ 4 3 2 0 1 ]
  
  [ 9 8 7 5 6 ]
  </code></pre>
  <p>I have tested my heap and other parts well enough, I am pretty sure the problem is on memory allocation but I couldn't find out where the mistake is.</p>
  <pre><code>int main(int argc, char *argv[])
  {        
      int M = atoi(argv[1]);
      int pq_1_size = M / 2;
      int pq_2_size = M - pq_1_size;
  
      int *a;
      int *b;
      a = (int *)malloc(pq_1_size * sizeof(int));
      b = (int *)malloc(pq_2_size * sizeof(int));
      for (int i = 0; i &lt; pq_1_size; i++) {
          a[i] = i;
      } 
      for (int j = pq_1_size; j &lt; M; j++) {
          b[j] = j;
      }  
      //int a[5] = { 0, 1, 2, 3, 4 }; // if I use this section instead it works fine.
      //int b[5] = { 5, 6, 7, 8, 9 }; // if I use this section instead it works fine.
      Heap someHeap = { 0, {0} };
      Heap *A = &amp;someHeap;
      buildHeap(A, a, pq_1_size);
      //buildHeap(A, a, 5);
      print(A);
      printf(&quot;\n&quot;);   
  
      Heap anotherHeap = { 0, {0} };
      Heap *B = &amp;anotherHeap;
      buildHeap(B, b, pq_2_size);
      //buildHeap(B, b, 5);
      print(B);
      return 0;
  }
  </code></pre>
  <p>Below is the heap code:</p>
  <pre><code>#define HEAPSIZE 500
  #define left(i) ((i)&lt;&lt;1)
  #define right(i) (((i)&lt;&lt;1)+1)
  #define parent(i) ((i)&gt;&gt;1)
  
  typedef struct {
      int size;
      int element[HEAPSIZE];
  } Heap;
  
  //Swaps the values of two ints a and b
  void swap(int * a, int * b) {
      int temp;
      temp = * a;
      * a = * b;
      * b = temp;
  }
  
  //Ensures that the max heap property in heap A is satisfied at and below index i
  void makeHeap(Heap * A, int i) {
      int largest = A-&gt;element[i];
      int position = i;
      int l = left(i);
      if(l &lt;= A-&gt;size &amp;&amp; A-&gt;element[l] &gt; largest) {
          largest = A-&gt;element[l];
          position = l;
      }
      int r = right(i);
      if(r &lt;= A-&gt;size &amp;&amp; A-&gt;element[r] &gt; largest) {
          largest = A-&gt;element[r];
          position = r;
      }
      if(i != position) {
          swap(&amp;A-&gt;element[i], &amp;A-&gt;element[position]);
          makeHeap(A, position);
      }
  }
  
  //Get the maximum value in the heap A
  int extractMax(Heap * A) {
      if(!A-&gt;size) {
          puts(&quot;error: heap empty&quot;);
          return 0;   
      }
      int max = A-&gt;element[0];
      swap(&amp;A-&gt;element[0], &amp;A-&gt;element[A-&gt;size]);
      --A-&gt;size; 
      makeHeap(A, 0);
      return max;
  }
  
  //Increase the key of the ith element in heap A to be k
  void increaseKey(Heap * A, int i, int k) {
      if(A-&gt;element[i] &gt;= k) {
          printf(&quot;error: %d is less than the key of %d\n&quot;, k, i);
          return;
      }
      int position = i;
      while(position != 0 &amp;&amp; A-&gt;element[parent(position)] &lt; k) {
          A-&gt;element[position] = A-&gt;element[parent(position)];
          position = parent(position);
      }
      A-&gt;element[position] = k;
  }
  
  //Inserts the value i into the heap A
  void insert(Heap * A, int i) {
      if(A-&gt;size &gt;= HEAPSIZE) {
          printf(&quot;error: heap full\n&quot;);
          return;
      }
      A-&gt;element[A-&gt;size] = INT_MIN;
      increaseKey(A, A-&gt;size, i);
      ++A-&gt;size;
  }
  
  //Prints the heap A as an array
  void print(Heap * A) {
      int i = 0;
      printf(&quot;[ &quot;);
      for(i = 0; i &lt; A-&gt;size; i++) {
          printf(&quot;%d &quot;, A-&gt;element[i]);
      }
      printf(&quot;]\n&quot;);
  }
  
  //Makes a heap out of an unsorted array a of n elements 
  void buildHeap(Heap * A, int * a, int n) {
      if(n &gt; HEAPSIZE) {
          printf(&quot;error: too many elements\n&quot;);
          return;
      }
      if(A-&gt;size) {
          printf(&quot;error: heap not empty\n&quot;);
          return;
      }
      int nbytes = n * sizeof(int);
      memcpy(A-&gt;element, a, nbytes);
      A-&gt;size = n;
      int i = 0;
      for(i = A-&gt;size/2; i &gt;= 0; i--) {
          makeHeap(A, i);
      }
  }
  </code></pre>