Anthony Ghosn
Anthony Ghosn

Reputation: 82

exception thrown error. why is it happening? and what should i do to fix it?

int main() {
    int** a;
    int l, h;
    cout << "the lenght of the matrix is= ";
    cin >> l;
    cout << "the height of the matrix is= ";
    cin >> h;
    a = new int* [l];
    a[l] = new int [h];
    //a = new int [l][h];
    if (l = h) {
        Pn(l,a);
    }
}

void Pn(int l,int** a) {
    intMatrix(l, l, a);
}

void intMatrix(int l, int h, int** a) {
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < l; j++)
            a[i][j] = 0;   //the exception error
    }
}

this is a part of my code where the error is happening. what is this error and how can i fix it? also yes i am putting the same numbers for 'l' and 'h'.

Upvotes: 0

Views: 479

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 598114

You are not allocating the array correctly.

After a = new int* [l];, you try to access a[l], which is out of bounds.

You need to allocate a separate int[] for each element of the array's 2nd dimension.

Even if you were doing that correctly, there are other problems in the code:

  • leaking the array.

  • if (l = h) is using the = assignment operator, thus assigning the value of h to l, and then comparing the new value of l against 0. To compare l to h, you need to use the == comparison operator instead.

  • the loop in intMatrix() is backwards. The outer loop needs to iterate through l, and the inner loop needs to iterate through h, not the other way around. The only way intMatrix() would work correctly is if l and h have the same value, which your if is (incorrectly) trying to force. But the user is not being forced to enter 2 of the same value.

With that said, try something more like this:

#include <iostream>
using namespace std;

void Pn(int l, int** a);
void intMatrix(int l, int h, int** a);

int main() {
    int** a;
    int l, h;

    cout << "the length of the matrix is= ";
    cin >> l;
    cout << "the height of the matrix is= ";
    cin >> h;

    a = new int* [l];
    for(int i = 0; i < l; ++i) {
        a[i] = new int [h];
    }

    if (l == h)
        Pn(l, a);
    else
        intMatrix(l, h, a);

    for(int i = 0; i < l; ++i) {
        delete[] a[i];
    }
    delete[] a;
}

void Pn(int l, int** a) {
    intMatrix(l, l, a);
}

void intMatrix(int l, int h, int** a) {
    for (int i = 0; i < l; ++i) {
        for (int j = 0; j < h; ++j)
            a[i][j] = 0;
    }
}

Though, you might consider using a 1-dimensional contiguous array instead of a 2-dimensional sparse array, eg:

#include <iostream>
using namespace std;

void Pn(int l, int* a);
void intMatrix(int l, int h, int* a);

int main() {
    int* a;
    int l, h;

    cout << "the length of the matrix is= ";
    cin >> l;
    cout << "the height of the matrix is= ";
    cin >> h;

    a = new int [l*h];

    if (l == h)
        Pn(l, a);
    else
        intMatrix(l, h, a);

    delete[] a;
}

void Pn(int l, int* a) {
    intMatrix(l, l, a);
}

void intMatrix(int l, int h, int* a) {
    for (int i = 0; i < l; ++i) {
        for (int j = 0; j < h; ++j)
            a[(i*l)+j] = 0;
    }
}

That being said, consider using std::vector instead of new[] manually. For instance, for the 2-dimensional array:

#include <iostream>
#include <vector>
using namespace std;

void intMatrix(vector<vector<int>> &a);

int main() {
    vector<vector<int>> a;
    int l, h;

    cout << "the length of the matrix is= ";
    cin >> l;
    cout << "the height of the matrix is= ";
    cin >> h;

    a.resize(l);
    for(int i = 0; i < l; ++i) {
        a[i].resize(h);
    }

    intMatrix(a);

    // or simply:
    // a.resize(l, vector<int>(h, 0));
}

void intMatrix(vector<vector<int>> &a) {
    for (size_t i = 0; i < a.size(); ++i) {
        vector<int> &b = a[i];
        for (size_t j = 0; j < b.size(); ++j)
            b[j] = 0;
    }
}

Or, for the 1-dimensional array:

#include <iostream>
#include <vector>
using namespace std;

void intMatrix(vector<int> &a);

int main() {
    vector<int> a;
    int l, h;

    cout << "the length of the matrix is= ";
    cin >> l;
    cout << "the height of the matrix is= ";
    cin >> h;

    a.resize(l*h);
    intMatrix(a);

    // or simply:
    // a.resize(l*h, 0);
}

void intMatrix(vector<int> &a) {
    for (size_t i = 0; i < a.size(); ++i) {
        a[i] = 0;
    }
}

Upvotes: 3

Related Questions