How to modify dynamic matrix?

Asked

Viewed 162 times

1

I’m having trouble modifying a dynamic matrix already created. The following example depicts my problem. In this example the program compiles but gives error of execution ( nothing appears and shows the dialog saying that the program stopped working).

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

void Alocar(int **m,int rows,int cols){

    m = (int **)malloc((rows)*sizeof(int*));

    for(int i=0;i<rows;i++){
         m[i] = (int *)malloc(cols*sizeof(int));   
    }

}

void Modificar(int **m,int rows,int cols){


     for(int i=0; i<rows ;i++)
     {
        for(int j=0; j<cols ; j++)
        {

          m[i][j]= 0;
        }
     }
     cout << " Matriz zerada" << endl;
      for(int i=0; i<rows ;i++)
     {
        for(int j=0; j<cols ; j++)
        {

          cout << m[i][j]<< " ";
        }
        cout << endl;
     }

}

int main()
{
     int **M;

     Alocar(M,100,100);

     Modificar(M,100,100);

    return 0;

}

Does anyone know why this kind of problem is occurring?

  • In C+= should not do so, should use a vector or other structure. This program is in C in C++.

1 answer

1


The problem in the code shown comes in the function Alocar. When does m = ... within the function Alocar is changing a copy of the pointer in the main what makes the main continue without being initialized.

It can solve this problem in some ways:

  • Returning the new pointer as return

    int** Alocar(int **m,int rows,int cols) {  
    //^---tipo de retorno para int**
    
        m = (int **)malloc((rows)*sizeof(int*));
    
        for(int i=0; i<rows; i++) {
            m[i] = (int *)malloc(cols*sizeof(int));
        }
    
        return m; //retorna aqui
    }
    
    int main() {
        int **M = Alocar(M,100,100); //guarda o retornado em m
        Modificar(M,100,100);
    
        return 0;
    }
    

    Example in Ideone
    I changed the amount to 10 to be easier to see

  • Passing the address of the matrix to the function:

    void Alocar(int ***m,int rows,int cols) {
        //----------^ agora int***
    
        *m = (int **)malloc((rows)*sizeof(int*)); //com * para ser o valor apontado
    
        for(int i=0; i<rows; i++) {
            (*m)[i] = (int *)malloc(cols*sizeof(int)); //com * tambem
        }
    }
    
    int main() {
        int **M;
    
        Alocar(&M,100,100); //passa o endereço agora
        Modificar(M,100,100);
    
        return 0;
    
    }
    

    Example also in Ideone

  • Exclusively in C++ has even the idiomatic passage by reference that simplifies even more. The compiler internally ends up treating it like a pointer to the original:

    void Alocar(int **&m,int rows,int cols) {
        //------------^ referencia aqui o resto igual ao que tinha originalmente
        m = (int **)malloc((rows)*sizeof(int*));
    
        for(int i=0; i<rows; i++) {
            m[i] = (int *)malloc(cols*sizeof(int));
        }
    }
    
    int main() { //o main mantem-se igual
        int **M;
    
        Alocar(M,100,100);
        Modificar(M,100,100);
    
        return 0;
    }
    

    Final example in Ideone

  • Perfect. Thanks for the response with several alternatives.

Browser other questions tagged

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