#include #include void desallocation(void* ptr) { if (ptr != NULL) { printf("Desallocation a l'adresse : %p\n", ptr); free(ptr); } else { printf("Attention, tentative de desallocation de NULL"); } } int main(void) { size_t taille1 = 3; size_t taille2 = 5; size_t taille3 = 2; int* ptr = (int*) malloc(taille1 * sizeof(int)); int* tmp; if (ptr == NULL) { printf("Allocation avortee\n"); exit(1); } printf("Allocation reussie a l'adresse : %p\n", ptr); for (int i = 0; i < taille1; i++) { ptr[i] = i; } tmp = (int*) realloc(ptr, taille2 * sizeof(int)); if (tmp == NULL) { printf("Reallocation avortee\n"); desallocation(ptr); ptr = NULL; exit(2); } ptr = tmp; printf("Reallocation reussie a l'adresse : %p\n", ptr); tmp = (int*) realloc(ptr, taille3 * sizeof(int)); if (tmp == NULL) { printf("Reallocation avortee\n"); desallocation(ptr); ptr = NULL; exit(3); } ptr = tmp; printf("Reallocation reussie a l'adresse : %p\n", ptr); desallocation(ptr); ptr = NULL; return 0; }