title: C programming: SIGABRT 134 error; am I freeing this memory correctly?
body: <p>I have a nested datastructure as such (these are the initalizations):</p>
  
  <pre><code>Interval *newInterval(int b, int e, int m){
      static Interval i;
      i.b = b;
      i.e = e;
      i.m = m;
      return &amp;i;
  }
  Signal *newSignal(int size){
      static Signal s;
      s.intervals = malloc(size * sizeof(Interval));
      s.top = 0;
      s.size = size;
      return &amp;s;
  }
  
  Stack *newStack(int size){
      static Stack st;
      st.signals = malloc(size * sizeof(Signal));
      st.top = 0;
      st.size = size;
      return &amp;st;
  }
  </code></pre>
  
  <p><strong>How they the data is structured:</strong> Multiple Interval struct pointers are stored in the 'intervals' array of each Signal struct. Each Signal struct pointer is stored in the 'signals' array of a Stack struct (there will always only be one).</p>
  
  <pre><code>        -&gt; [Signal 0] -&gt; [Interval 0][Interval 1][Interval 2]...
  [Stack] -&gt; [Signal 1] -&gt; [Interval 0][Interval 1]
          -&gt; [Signal 2] -&gt; [Interval 0][Interval 1][Interval 2]...
  </code></pre>
  
  <p><strong>How I try to access and free data:</strong> I have two functions that print out data from within the structures, and attempt to free the memory allocated using malloc (and realloc). However I seem to be getting a SIGABRT (error 134). I suppose this is because of how I free the memory; but I don't know what to do!</p>
  
  <pre><code>    void pop(Signal *s, int n){
      if(n &lt; s-&gt;top) {
          printf("[%d,%d)@%d ", s-&gt;intervals[n].b, s-&gt;intervals[n].e, s-&gt;intervals[n].m);
          pop(s,n+1);
      }
      else {
          free(s-&gt;intervals);
      }
  }
  
  void printIntervals(Stack *st){
      for(int i=0; i&lt;st-&gt;top; i++){
          pop(&amp;(st-&gt;signals[i]), 0);
          printf("\n");
          free(&amp;(st-&gt;signals[i]));
      }
  }
  </code></pre>
  
  <blockquote>
    <p><strong>1.</strong> printIntervals is called once, and in turn calls the pop function for each signal.</p>
    
    <p><strong>2.</strong> The pop function recursively prints out each interval struct, until the array containing intervals is empty, at which point I
    attempt to free the intervals array from the signal.</p>
    
    <p><strong>3.</strong> At this point pop finishes, and the flow goes back to printIntervals. Here, I attempt to free the signals array as there's
    nothing left to print, and the loop moves on to print the next signal
    from the Stack.</p>
  </blockquote>
  
  <p>Is the way I free memory incorrect? How can I fix the memory error? Thank you.</p>