i have to do a dynamic-allocation (double-pointer) in a function and then i have to put in the matrix some number and then deallocate it
i have also tried to write **matrice instead of *(matrice[i]) in first function when i have to create a matrix after the first dynamic-allocation but it is always wrong and in this case the program exit after i insert the first line of the matrix
#include <iostream>
#include <stdlib.h>
using namespace std;
void allocaMatrice(int *** matrice,int * r,int * c);
void leggiMatrice(int ** matrice,int r,int c);
void scriviMatrice(int ** matrice,int r,int c);
void deallocaMatrice(int ** matrice,int r);
int main (){
int **matrice;
int r, c;
allocaMatrice(&matrice,&r,&c);
leggiMatrice(matrice,r,c);
scriviMatrice(matrice,r,c);
deallocaMatrice(matrice,r);
cin.get();
return 0;
}
void allocaMatrice(int *** matrice,int * r,int * c){
cout<<"Dimmi il numero di righe della matrice"<<endl;
cin>>*r;
while(cin.get()!='\n');
cout<<"Dimmi il numero di colonne della matrice"<<endl;
cin>>*c;
while(cin.get()!='\n');
*matrice=(int **) calloc (*r,sizeof(int));
for(int i=0;i<*c;i++){
*matrice[i]=(int *) malloc ((*c)*sizeof(int));
}
}
void leggiMatrice(int ** matrice,int r,int c){
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
cout<<"Dimmi l'elemeno alla riga e colonna corrente"<<endl;
cin>>matrice[i][j];
while(cin.get()!='\n');
}
}
}
void scriviMatrice(int ** matrice,int r,int c){
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
cout<<matrice[i][j]<<"\t";
}
cout<<endl;;
}
}
void deallocaMatrice(int ** matrice,int r){
int i;
for(i=0;i<r;i++){
free((*matrice)+1);
}
free(matrice);
}
the debug error is "Program received signal SIGSEGV, Segmentation fault"