0
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'.

  • 1
    Something noteworthy: [Why should I always enable compiler warnings?](https://stackoverflow.com/q/57842756/962089) – chris Jan 07 '22 at 00:41
  • This doesn't compile as-is (missing includes, `Pn()` is used before it is defined) which makes it a lot harder to comment on than needed. Please provide a Minimal, Complete, Reproducible Example. – Adriaan de Groot Jan 07 '22 at 00:50
  • 1
    `if(l=h)` does not do what you think it does. Well, maybe it's intentional, but then why bother to ask for `h` in a MCRE? – Adriaan de Groot Jan 07 '22 at 00:51
  • 1
    `a` is a pointer-to-ponter-to-int. You've allocated space for `l` pointer-to-ints. Then you allocate `h` pointer-to-ints and assign that to `a[l]` with `a[l] = new int[h]`. That's out-of-bounds: the greatest valid index is `l-1`. You also have not allocated any pointer-to-ints for all the **other** places in `a`. So you have `a[0]` through `a[l-1]` pointing nowhere. – Adriaan de Groot Jan 07 '22 at 00:53
  • *"what is this error"* -- this is what you should be telling us, not us telling you. What is the error message? What debugging have you done to identify the spot where you think the error occurs? Are you able to reproduce the error consistently? If so, why does your [mre] require user input? – JaMiT Jan 07 '22 at 01:56

1 Answers1

3

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;
    }
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770