1

I was making a cpp program in which it takes two input from the user which determine the size of the 2d array and pass the values to the mat class constructor and dynamically create an array of the user's defined size. But, I don't know why it is not working and showing segmentation fault

#include<iostream>
using namespace std;

class mat{
    int **a;
    int r, c;
    public:
        mat(int row, int col){
            r = row;
            c = col;
            
            for(int i = 0; i < r; i++){
                *a = new int[r];
                *a++;
            }
            
        }
        void input(){
            for(int i = 0; i < r; i++){
                for(int j = 0; i < c; j++){
                    cin >> a[i][j];
                }
            }
        }
        void display(){
            for(int i = 0; i < r; i++){
                for(int j = 0; i < c; j++){
                    cout << a[i][j] << "\t";
                }
                cout << endl;
            }
        }
    
};

int main()
{
    int r, c;
    cout << "enter row :";
    cin >> r;
    cout << "enter column :";
    cin >> c;
    mat m(r, c);
    m.input();
    cout << "array \n";
    m.display();
}

I can feel that the issue is with the for loop in the constructor or maybe I am doing it wrong.

  • 2
    You need to initialize `a`. – vandench Oct 21 '21 at 13:54
  • Since a, r and c are private, you can use a flat `vector a` and calculate it's 1D size using `r * c`. Then translate the 2D x,y into `x + y * c` when accessing the 1D vector from the 2D coordinates. – Eljay Oct 21 '21 at 13:56
  • you just allocated memory just to hold array of pointers of size r, but you never allocated any memory to hold actual data pointed by those pointers. – foragerDev Oct 21 '21 at 14:10
  • 2
    Does this answer your question? https://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new – 463035818_is_not_an_ai Oct 21 '21 at 14:12
  • 1
    What do you think `a` is pointing to when you do `*a = new int[r];`? (And once you've fixed that, what do you think it points to when the loop has finished?) – molbdnilo Oct 21 '21 at 14:14

1 Answers1

1

The class contains several errors.

  • The variable a is never initialized. When we try to address the memory pointed to by a we get a segmentation fault. We can initialize it like this a = new int*[r]
  • We should not change where a point's to, so don't use a++. Otherwise a[i][j] will not refer to the i'th row and the j'th column. We would also want to release the memory at some point.
  • The inner loop for the columns for(int j = 0; i < c; j++) once entered will never terminate and will eventually produce a segmentation fault. We need to change i < c to j < c.

If we fix these errors, it looks like this:

class mat {
    int** a;
    int r, c;
public:
    mat(int row, int col) {
        r = row;
        c = col;

        a = new int*[r];
        for (int i = 0; i < r; i++) {
            a[i] = new int[c];
        }
    }
    void input() {
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                cin >> a[i][j];
            }
        }
    }
    void display() {
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                cout << a[i][j] << "\t";
            }
            cout << endl;
        }
    }
};
GoWiser
  • 857
  • 6
  • 20
  • actually I fixed all these issues after posting the question with the help of geeksforgeeks, still thank you so much for your answer. – Rakesh Kr Yadav Oct 21 '21 at 14:45