Heap Corruption Detected: after Normal block (#126)
===

I cannot for the life of me figure out why I am getting this Debug Error:

Heap Corruption Detected: after Normal block (#126) at
0x004cF6c0
CRT detected that the application wrote to memory after end of heap bugger.

I understand that you need to free memory whenever you use new operator,
which I did and I am still getting problems. 

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.

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.

Why is this occurring?


     /*this program calculates the sum of all the numbers in the array*/

    #include <iostream>
    #include <time.h>
    
    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<<"Enter a number into an array: ";
		cin>>array[j];

		cout<<"add another?(y/n):";
		char choice;
		cin>>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 < size){
		system("cls");

		cout<<"The sum is  :"<<endl;
		sum += array[i];
		cout<<"...."<<sum;


		time_t start_time, stop_time;  
		time(&start_time);

		do
		{
			time(&stop_time); //pause for 5 seconds
		}
		while((stop_time - start_time) < 5);

		countSum(sum, (i+1) , size, array); //call recursive function	
	}


}
