Take the 2-minute tour ×

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;

void countSum(int, int, int, int*);

int main(){

    bool flag;
    int num;

    int sum = 0, j=0;
    int *array =  new int;


        cout<<"Enter a number into an array: ";

        cout<<"add another?(y/n):";
        char choice;
        choice == 'y' ? flag = true : flag = false;



    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){

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

        time_t start_time, stop_time;

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

        countSum(sum, (i+1) , size, array); //call recursive function
share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

array holds enough space for a single int:

int *array = new int;

but there is potentially an attempt to insert more than one int which would result in writing to memory that is not available. Either use a std::vector<int> or it must be known beforehand the maximum number of ints that will be entered before array is allocated.

If this is a learning exercise and you do not want to use std::vector<int> you could prompt the user to enter the number of ints they wish to enter:

std::cout << "Enter number of integers to be entered: ";
int size = 0;
std::cin >> size;
if (size > 0)
    array = new int[size];

Then accept size number of ints. Use delete[] when you use new[].

share|improve this answer
I thought the whole point of creating a dynamic array was so that you could add more. I never declared the array as const int array[0] –  user1066524 Jul 19 '12 at 21:23
It means that the size does not need to be known at compile time, but it won't grow automatically: a std::vector<int> would. –  hmjd Jul 19 '12 at 21:24
yeah I know vectors are better. my teacher of course asked for an array....even though arrays are incredibly annoying and not very helpful when it comes to data. –  user1066524 Jul 19 '12 at 21:28
can someone explain to me what is the point of a dynamic array? I know it can expand an already allocated array....but that just seems unrealistic because not every scenario is going to have a predetermined number. –  user1066524 Jul 19 '12 at 21:31
How do you expand an already allocated array?? –  hmjd Jul 19 '12 at 21:34
show 5 more comments

The solution was to set the size new int[size]....although I wish that you didn't have to set a size if it is dynamic.

share|improve this answer
add comment

Your Answer


