Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heap corruption detected error in C++

Here is my code:

#include<iostream>
#include<cstdlib>

using namespace std;

int main() {
    int** arr=NULL;
    int num=0;
    cin >> num;
    int* big=NULL;
    arr = new int*[num];
    for (int i = 0; i < num; i++) {
        arr[i] = new int[5];
    }
    big = new int[num];

    for (int i = 0; i < num; i++) {
        for (int j = 0; j < 5; j++) {
            while (1) {
                cin >> arr[i][j];
                if (arr[i][j] >= 0 && arr[i][j] < 100)
                    break;
            }
        }
    }

    for (int i = 0; i < 5; i++) {
        big[i] = 0;
    }

    for (int i = 0; i < num; i++) {
        for (int j = 0; j < 5; j++) {
            if (big[i] < arr[i][j]) {
                big[i] = arr[i][j];
            }
        }
    }

    for (int i = 0; i < num; i++) {
        cout << "Case #" << i + 1 << ": " << big[i] << endl;
    }

    delete[]big;
    for (int i = num-1; i>=0; i--) {
        delete[]arr[i];
    }
    delete[]arr;

    return 0;
}

When I run this code, it says that there are heap corruption error (heap corruption detected). I think it means that there are some errors at 'new' or 'delete' parts in my codes, but I cannot find them. I hope someone to answer. Thanks.

like image 792
J. Shin Avatar asked Jan 23 '26 20:01

J. Shin


2 Answers

Error is here:

big = new int[num];
...
for (int i = 0; i < 5; i++) {
    big[i] = 0;
}

So when you have num less than 5 you are writing outside the array.

Anyway you are using C++ so use vector for such tasks.

#include<iostream>
#include<cstdlib>
#include<vector>

using namespace std;

int main() {
    vector<vector<int>> arr;
    int num=0;
    cin >> num;
    arr.resize(num, vector<int>(5));

    for (auto &row : arr) {
        for (auto &cell : row) {
            while (1) {
                cin >> cell ;
                if (cell >= 0 && cell < 100)
                    break;
            }
        }
    }

    vector<int> big(arr.size());
    for (int i = 0; i < arr.size(); i++) {
        for (auto &cell : arr[i]) {
            if (big[i] < cell) {
                big[i] = cell;
            }
        }
    }

    for (int i = 0; i < num; i++) {
        cout << "Case #" << i + 1 << ": " << big[i] << endl;
    }

    return 0;
}
like image 183
Marek R Avatar answered Jan 26 '26 12:01

Marek R


In many places in your code, you're indexing your big array using indexes from 0 to 5, while the array is allocated using user input, if user input was 4 for example, your code is undefined behavior.

If you're using c++, you shouldn't be manually allocating the arrays, use std::vector instead, it will take care of managing memory for you, so you don't have to new and delete memory yourself.

With std::vector, your code would look somewhat like this.

std::vector<std::vector<int>> arr;
std::vector<int> big;
cin>>num;
arr.resize(num, std::vector<int>(5));
big.resize(5);

You will also be able to use at method to access elements while bound-checking, and size method to get the number of elements of the array.

like image 38
Kaldrr Avatar answered Jan 26 '26 12:01

Kaldrr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!