title: BubbleUp method in Heap implementation in c
body: <p>I posted my heap implementation here earlier and got some much needed help with my add() function, which I'm beyond grateful for, but I'm still getting invalid reads and memory leaks, and could use further assistance. </p>
  
  <p>heap.h header file:</p>
  
  <pre><code>#ifndef HEAP_H
  #define HEAP_H
  #include &lt;stdbool.h&gt;
  struct Entry {
    int key;
    char* value;
  };
  
  typedef struct Entry Entry;
  
  struct Heap {
    int capacity;
    int size;
    Entry** elements;
  };
  
  typedef struct Heap Heap;
  
  Heap* makeHeap(int capacity);
  void add(Heap* heap, int priority, char* value);
  char* removeMin(Heap* heap);
  char* peek(Heap* heap);
  int size(Heap* heap);
  void cleanupHeap(Heap* h);
  
  #endif
  </code></pre>
  
  <p>heap.c file:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdbool.h&gt;
  #include "heap.h"
  
  Heap* makeHeap(int capacity) {
     //Make the heap
     Heap* theHeap = calloc(1, sizeof(Heap));
     //set its capacity to param
     theHeap-&gt;capacity = capacity;
     //inital size is 0
     theHeap-&gt;size = 0;
     //elements contains pointers (references) to Entry objects.
     theHeap-&gt;elements = calloc(capacity, sizeof(Entry*));
     //iterate capacity times allocating an entry reference for each element to be placed
     int i = 0;
     for(; i &lt; capacity; i++) {
       theHeap-&gt; elements[i] = calloc(1, sizeof(Entry));
     }
  
     return theHeap;
  }
  
  void expandCapacity(Heap* h) {
     Entry** oldcontents = h-&gt;elements;
     Entry** newarr = calloc(h-&gt;capacity * 2, sizeof(Entry*));
     int i = 0;
     for(; i &lt; h-&gt;size; i += 1) {
        newarr[i] = h-&gt;elements[i];
     }
  
     h-&gt;capacity = h-&gt;capacity * 2;
     h-&gt;elements = newarr;
     free(oldcontents);
  }
  
  void add(Heap* h, int priority, char* val) {
    if (h-&gt;size &gt;= h-&gt;capacity) {
      expandCapacity(h);
    }
    //insert at end of storage array and bubble up
    Entry* toAdd = calloc(1, sizeof(Entry));
    toAdd-&gt;key = priority;
    toAdd-&gt;value = val;
    h-&gt;elements[h-&gt;size]=toAdd;
    h-&gt;size += 1;
    bubbleUp(h, h-&gt;size);
  }
  
  void bubbleUp(Heap* h, int index) {
     if(index &lt;= 0) { return; }
     Entry* e = h-&gt;elements[index];
     Entry* parent = h-&gt;elements[(index-1)/2];
     int comp = strcmp(e-&gt;value, parent-&gt;value);
     if(comp &gt; 0) {
       swap(h, index, parent-&gt;key);
       bubbleUp(h, parent-&gt;key);
     }
     else {
       return;
     }
  }
  
  void swap(Heap* h, int index1, int index2) {
     Entry* tmp = h-&gt;elements[index1];
     h-&gt;elements[index1] = h-&gt;elements[index2];
     h-&gt;elements[index2] = tmp;
  }
  void cleanupHeap(Heap* h) {
     int i = 0;
     for (; i &lt; h-&gt;capacity; i++) {
        free(h-&gt;elements[i]);
     }
     free(h-&gt;elements);
     free(h);
  }
  </code></pre>
  
  <p>bubbleUp() and swap() were given functions in Java, so all I had to do was adapt them for this project to work with the heap.h file, and of course translate them to c. I'm confident in add() from my earlier assistance, and swap() also as it seems pretty straightforward, but I'm thinking there's an issue with my translation to c for bubbleUp(). Any input is greatly appreciated.</p>
  
  <p>Java Code:</p>
  
  <pre><code> void bubbleUp(int index) {
      if(index &lt;= 0) { return; }
      Entry&lt;K,V&gt; e = this.entries.get(index);  
      Entry&lt;K,V&gt; parent = this.entries.get(parent(index));  
      int comp = this.comparator.compare(e.key, parent.key);
      if(comp &gt; 0) {
        swap(index, parent(index));
        bubbleUp(parent(index));
      }
      else {
        return;
      }
  }
  
  void swap(int i1, int i2) {
      Entry&lt;K,V&gt; tmp = this.entries.get(i1);
      this.entries.set(i1, this.entries.get(i2));
      this.entries.set(i2, tmp);
  }
  </code></pre>
  
  <p>Test:</p>
  
  <pre><code>#include "cutest/CuTest.h"
  #include "heap.h"
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  void TestAdd(CuTest *tc) {
    Heap* h = makeHeap(10);
    add(h, 4, "lol");
    CuAssertIntEquals(tc, 1, h-&gt;size);
    cleanupHeap(h);
  }
  
  CuSuite* StrUtilGetSuite() {
    CuSuite* suite = CuSuiteNew();
    SUITE_ADD_TEST(suite, TestAdd);
    return suite;
  }
  
  // Don't edit below this line
  
  void RunAllTests(void) {
    CuString *output = CuStringNew();
    CuSuite* suite = CuSuiteNew();
    CuSuite* ourSuite = StrUtilGetSuite();
  
    CuSuiteAddSuite(suite, ourSuite);
  
    CuSuiteRun(suite);
    CuSuiteSummary(suite, output);
    CuSuiteDetails(suite, output);
    printf("%s\n", output-&gt;buffer);
  
    CuStringDelete(output);
    CuSuiteDelete(suite);
    free(ourSuite);
  }
  
  int main(void) {
    RunAllTests();
    return 0;
  }
  </code></pre>