#include #include int** creerMatrice(size_t nbLignes, size_t nbColonnes) { int** ti = (int**) calloc(nbLignes, sizeof(int*)); if (ti != NULL) { int i = 0; while ((i < nbLignes) && ((ti[i] = (int*) calloc(nbColonnes, sizeof(int))) != NULL)) { i++; } if (ti[nbLignes - 1] == NULL) { for (int j = 0; j < i; j++) { free(ti[j]); ti[j] = NULL; } free(ti); ti = NULL; } } return ti; } void detruireMatrice(int** ti, size_t nbLignes, size_t nbColonnes) { if (ti != NULL) { for (int i = 0; i < nbLignes; i++) { free(ti[i]); ti[i] = NULL; } free(ti); ti = NULL; } } void test(size_t nbL, size_t nbC) { printf("Tentative d'allocation pour %zu x %zu entiers\n", nbL, nbC); printf("soit %zu octets (environ %zu Go)\n", nbL * nbC * sizeof(int), (nbL * nbC * sizeof(int)) >> 30); int** ti = creerMatrice(nbL, nbC); if (ti != NULL) { printf("Allocation reussie\n"); detruireMatrice(ti, nbL, nbC); ti = NULL; printf("Desallocation terminee\n"); } else { printf("Allocation avortee\n"); } } void main(void) { test(100, 500000000); printf("\n"); test(1000, 500000); }