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
    }
}

2 Answers

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[].

9

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.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Elena Rostova

Elena Rostova

Lead Health, Wellness & Medical Journalist

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.

Share this article
Twitter Facebook Pinterest