Allocation dynamique sur tableau 2D
Bonjour, j'aimerais faire une fonction qui alloue dynamiquement un tableau 2D mais en seulement 2 malloc et la même fonction en seulement 1 malloc. J'ai déjà tenté ça mais le programme crash et je ne sais pas pourquoi. Merci d'avance.
En 1 malloc
Code:
1 2 3 4 5 6
| int** initTab(int N, int M){
int** tab = (int**) malloc( (sizeof(int*)*N*M );
return tab;
} |
En 2 malloc
Code:
1 2 3 4 5 6 7 8
| int** initTab(int N, int M){
int** tab = (int**) malloc(sizeof(int*)*N);
int** tab2 = (int*) malloc(sizeof(int)*M);
return tab;
} |