Home c++ heap corruption detected after normal block C++

heap corruption detected after normal block C++

Author

Date

Category

I started learning C++ a couple of days ago and faced such a problem. The goal is to write a program to enter numbers and calculate their average. I organized a dynamic array for this case and I write user input into it, but the heap corruption detected after normal block error constantly pops up. I don’t understand where the wrong section is located at all.

#include <iostream>
int main()
{
    using namespace std;
    int numbersInArray = 0;
    double *input = new double [numbersInArray];
    double test;
    while(numbersInArray < 10)
    {
        cin >> test;
        if (!cin)
            break;
        else
        {
            numbersInArray++;
            input[numbersInArray - 1] = test;
        }
    }
    double average = 0;
    for (int i = 0; i < numbersInArray; i++)
    {
        average += input[i];
    }
    cout << "Average value: " << average/numbersInArray << endl;
    int numbersGreaterThenAverage = 0;
    for (int i = 0; i < numbersInArray; i++)
    {
        if (input[i] > average)
            numbersGreaterThenAverage++;
    }
    cout << "Numbers greater then average: " << numbersGreaterThenAverage << endl;
    delete[] input;
    return 0;
}

Help, please, I rummaged half of Google, did not find a solution.


Answer 1

A dynamic array in C++ has two meanings: an array of variable size (i.e., capable of adding and removing elements in it) and an array that we allocated on the heap (heap). In this case, you are using the second kind of dynamic array, which by itself does not change its size.

When we want to read and write the elements of an array, then we have to choose which one to use: static or dynamic (1 or 2). If the number of elements is fixed (that is, we initially know), then a regular static array is perfect for this, but if the number of elements is entered from the keyboard, then we use a dynamic one, since according to the C++ standard, the number of elements in a static array must be known at the time of compilation, not execution.
In your case, as I understand it, the number of elements is fixed and equal to 10. Then a dynamic array is not required at all.

I will describe the solution in 3 types: using static and dynamic 1st and 2nd types.

Static (non-stretchable array allocated in stack):

int main()
{
    constexpr int ARR_SIZE = 10;
     arr[ARR_SIZE];
    for(int i = 0; i < ARR_SIZE; ++i)
        std::cin >> arr[i];
    //process array elements
    ...
    return 0;
}

Dynamic (not expandable array in heap):

#include <iostream>
#include <vector>
int main()
{
    int numbersInArray;
    std::cout << "Enter number of elements: ";
    std::cin >> numbersInArray;
    double *arr = new double[numbersInArray];
    for(int i = 0; i < numbersInArray; ++i)
        cin >> arr[i];
    //process array elements
    ...
    return 0;
}

Dynamic (stretchable array, internally highlighted in heap):

#include <iostream>
#include <vector>
int main()
{
    std::vector < double > arr;
    int numbersInArray;
    std::cout << "Enter number of elements: ";
    std::cin >> numbersInArray;
    for(int i = 0; i < numbersInArray; ++i)
    {
        double temp_variable;
        cin >> temp_variable;
        arr.push_back(temp_variable);
    }
    //process array elements
    ...
    return 0;
}

Answer 2, authority 100%

int numbersInArray = 0;
double *input = new double [numbersInArray];

So you’ve allocated memory for … zero elements. Hence all the troubles. Imagine that you were given a zero-room apartment – and where will you bring furniture? 🙂

If you really want to work not with a ready-made vector, for example, then allocate memory with a margin so that it will be enough. Once allocated, memory itself is not reallocated.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions