title: Number of disease cases related to patient age
body: <p>The user will give as input triples with the first number being 0, the second number being number of disease cases of a determined age (third number) OR first number being 1, the second and third numbers being the extremes of an age interval. I have to link "number of cases" with "age" and be able to edit these number of cases, it has the possibility to receive more cases for an age already quoted before (first number: 0). Besides that, it will give numbers X and Y, such that I have to print the number of cases related to the ages between X and Y (first number: 1). The language I am using is C.</p>
  
  <p>I tried this code</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int l[3], *list, n = 5;
  
  void add(int X, int Y){
      while (Y &lt; n){
          n = n * 2;
          list = realloc(list, n * sizeof(int));
      }
      list[Y] += X;
  }
  
  void consult(int X, int Y){
      int print = 0;
      for (int k = X; k &lt;= Y; ++k){
          print += list[k];
      }
      printf("%d\n", print);
  }
  
  int main() {
      int P;
      scanf("%d", &amp;P);
      list = (int*)calloc(n, sizeof(int));
      for (int i = 0; i &lt; P; ++i){
          for (int j = 0; j &lt; 3; ++j){
              scanf("%d", &amp;l[j]);
          }
          if (l[0] == 0){
              add(l[1],l[2]);
          } else {
              consult(l[1],l[2]);
          }
      }
      return 0;
  }
  </code></pre>
  
  <p>For the input:</p>
  
  <pre><code>20
  0 194 9
  1 7 41
  0 102 38
  1 30 43
  0 22 49
  1 34 56
  1 15 20
  1 34 57
  1 34 51
  0 128 39
  0 97 57
  0 114 25
  0 10 82
  0 127 35
  1 66 164
  0 60 85
  0 155 48
  1 60 186
  1 10 384
  0 166 950
  </code></pre>
  
  <p>it runs, giving back:</p>
  
  <pre><code>194
  102
  124
  0
  124
  124
  10
  70
  815
  
  [Program exited with exit code 0]
  </code></pre>
  
  <p>but for</p>
  
  <pre><code>40
  0 151 25
  0 5 3
  0 99 1
  0 109 14
  0 115 15
  0 110 1
  0 191 2
  0 94 9
  1 11 13
  1 25 31
  1 1 33
  1 14 26
  0 49 31
  1 28 33
  1 18 34
  1 16 23
  1 28 38
  1 13 26
  1 2 5
  1 17 29
  0 132 8
  1 21 24
  0 137 0
  1 0 13
  1 8 26
  1 12 20
  0 22 63
  1 5 8
  1 24 70
  1 51 61
  1 36 40
  0 164 42
  1 55 70
  0 134 112
  0 71 105
  0 101 7
  0 17 67
  1 56 252
  0 172 363
  0 18 188
  </code></pre>
  
  <p>it returns that "maximum stack or memory size exceeded,
  or maybe invalid pointer (code 139)".</p>
  
  <p>How should I fix this problem in my code?</p>
  
  <p>Observation: it's an obligation to start the vector with the size 5 and, if the size is not enough I need to duplicate the previous memory capacity.</p>