title: Heap Corruption Detected: after Normal block (#126)
body: <p>I cannot for the life of me figure out why I am getting this Debug Error:</p>
  
  <p>Heap Corruption Detected: after Normal block (#126) at
  0x004cF6c0
  CRT detected that the application wrote to memory after end of heap bugger.</p>
  
  <p>I understand that you need to free memory whenever you use new operator,
  which I did and I am still getting problems. </p>
  
  <p>for some reason the program doesn't end correctly in the recursive function.
  I debugged it and went through each line of code with breakpoints.</p>
  
  <p>At the end of the if statement in countSum it somehow subtracts 1 from i
  and then reenters the if block.....which it is not supposed to do.</p>
  
  <p>Why is this occurring?</p>
  
  <pre><code>/*this program calculates the sum of all the numbers in the array*/
  
  #include &lt;iostream&gt;
  #include &lt;time.h&gt;
  
  using namespace std;
  
  /*prototype*/
  void countSum(int, int, int, int*);
  
  int main(){
  
      bool flag;
      int num;
  
      int sum = 0, j=0;
      int *array =  new int;
  
      do{
  
          system("cls");
          cout&lt;&lt;"Enter a number into an array: ";
          cin&gt;&gt;array[j];
  
          cout&lt;&lt;"add another?(y/n):";
          char choice;
          cin&gt;&gt;choice;
          choice == 'y' ? flag = true : flag = false;
  
          j++;
  
      }while(flag);
  
      int size = j;
  
      countSum(sum, 0, size, array);
      //free memory
      delete array;
      array = 0;
  
      return 0;
  }
  
  void countSum(int sum, int i, int size, int *array){
  
      if (i &lt; size){
          system("cls");
  
          cout&lt;&lt;"The sum is  :"&lt;&lt;endl;
          sum += array[i];
          cout&lt;&lt;"...."&lt;&lt;sum;
  
          time_t start_time, stop_time;
          time(&amp;start_time);
  
          do
          {
              time(&amp;stop_time); //pause for 5 seconds
          }
          while((stop_time - start_time) &lt; 5);
  
          countSum(sum, (i+1) , size, array); //call recursive function
      }
  }
  </code></pre>