2

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.

trincot
  • 317,000
  • 35
  • 244
  • 286
J. Shin
  • 23
  • 3
  • 1
    I may compile your code at [cpp.sh](http://cpp.sh/) without any problem. What compiler are you using? We need some more info. Also what is the input, and what should the output be? – YesThatIsMyName Aug 27 '18 at 07:57
  • 2
    @YesThatIsMyName g++, clang and icc are compiling this (with -Wall) without warnings. – schorsch312 Aug 27 '18 at 07:59
  • 4
    A good modern C++ coding practice is to avoid `new` and `delete`, and use the standard library when possible, e.g. `std::vector` and `unique_ptr`. – rustyx Aug 27 '18 at 07:59
  • 6
    `big = new int[num];` `for (int i = 0; i < 5; i++) { big[i] = 0;` - so if `num` is less then 5 you have an error. – Marek R Aug 27 '18 at 08:01
  • 1
    Possible duplicate of [Heap corruption: What could the cause be?](https://stackoverflow.com/questions/1504251/heap-corruption-what-could-the-cause-be) – YesThatIsMyName Aug 27 '18 at 08:14
  • @MarekR Now, it works. Thanks a lot. – J. Shin Aug 27 '18 at 08:19

2 Answers2

4

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.

Matt
  • 2,554
  • 2
  • 24
  • 45
Kaldrr
  • 2,780
  • 8
  • 11
4

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;
}
Marek R
  • 32,568
  • 6
  • 55
  • 140