How to modify dynamic array?

1

I'm having trouble modifying a dynamic array already created. The following example depicts my problem. In this example the program compiles but gives execution error (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 type of problem is occurring?

    
asked by anonymous 11.11.2017 / 15:47

1 answer

1

The problem in the given code comes in the Alocar function. When you do m = ... within the Alocar function you are changing a copy of the pointer that is in main which causes main to continue without being initialized.

You can solve this problem in some ways:

  • Returning the new pointer as a 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 on Ideone
    I changed the amount to 10 to be easier to see

  • Passing the array address 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 ++ even has the idiomatic passage by reference that simplifies even more. The compiler internally ends up treating 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;
    }
    

    Ideone final example

11.11.2017 / 16:13