Why does the matrix not exist outside the scope of readMatriz()?

Asked

Viewed 23 times

0

I’m learning better C++ for marathons and I’m starting to use more methods instead of doing everything on main(), I need to read an array but I’m having trouble doing it through a method because this matrix doesn’t exist after the call of the function to read the whole matrix, that is, when it arrives at the printMatriz() of the segmentationFault because the matrix no longer exists in this scope.

#include <iostream>
using namespace std;

void readMatriz(int **M, int n) {
    M = new int *[n];
    for (int i = 0; i < n; i++) {
        M[i] = new int [n];
        for (int j = 0; j < n; j++) {
            cin >> M[i][j];
        }
    }
}

void printMatriz(int **M, int n) {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cout << M[i][j] << " ";
        }
        cout << endl;
    }
}

int main() {
    int n, **M;

    cin >> n;

    readMatriz(M, n);
    printMatriz(M, n);

    return 0;
}

Debugging the code I realize that I can call the function printMatriz() when I am still in the scope of readMatriz(), but outside it seems that the matrix no longer exists, which I find very strange because I am not passing value but reference.

If I take this part M = new int *[n]; which is in readMatriz() and move to main and code works perfectly and does not give segmentationFault.

Sorry some inconsistency is my first question posted here at Stackoverflow.

1 answer

0

The problem is because the value of M is passed by value. When you call the function readMatriz in main, the value of int** M is passed as a copy. It is as if a new int** was created and received the same value as M. When you change the contents of M, it changes only within the function. When leaving the function the value of the original variable M (da main) remains the same as it was before entering the function.

The simplest way to solve this is to pass the variable as a reference. This can be done as follows:

void readMatriz(int **&M, int n) {

Note that the only difference is that we have a & further after the type of the variable (int**). In this way any change to the variable within the function will also be reflected outside the function. Therefore, in the following allocation

M = new int *[n];

M will have the address of your matrix even after leaving the function.

Don’t forget that all memory allocated using the operator new is not automatically released when exiting scope. So before finishing the main function use the operator delete[] (with the same [] as it is an array) to release dynamically allocated memory. Follow the complete code:

#include <iostream>
using namespace std;

void readMatriz(int** &M, int n) {
    M = new int *[n];
    for (int i = 0; i < n; i++) {
        M[i] = new int [n];
        for (int j = 0; j < n; j++) {
            cin >> M[i][j];
        }
    }
}

void printMatriz(int **M, int n) {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cout << M[i][j] << " ";
        }
        cout << endl;
    }
}

int main() {
    int n, **M;

    cin >> n;

    readMatriz(M, n);
    printMatriz(M, n);

    for (int i = 0; i < n; ++i)
      delete[] M[i];
    delete[] M;

    return 0;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.