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++.– Maniero