Bon. J'ai fais à la va vite une fonction de réallocation qui permet de
redéfinir la taille de tout les pointeurs pointé et apporter aussi des
corrections (sécurités) à mes autres fonctions:
void **malloc2d (int num, int size)
{
void **tab, **c;
if(num <= 0 || size <= 0) return 0;
tab= (void**)malloc(sizeof(void*)*num); c = tab;
if(!c) return 0;
while(num > 0)
{
*c = (void*)malloc(size);
if(*c == 0) return 0;
c++; num--;
}
return tab;
}
void free2d (void **tab, int num)
{
void **c = tab;
if(c == 0 || num <= 0) return;
while(num > 0)
{
free(*c);
c++; num--;
}
free((void*)tab);
}
void **realloc2d (void **tab, int num, int newsize)
{
void **d;
if(tab == 0 || num <= 0 || newsize <= 0) return 0;
d = tab;
while(num > 0)
{
void *c = (void*)realloc(*d, newsize);
if(!c) return 0;
*d = c; d++; num--;
}
return tab;
}
Bon, voici un exemple:
char **TAB = (char**)malloc2d(2, 10);
strcpy(TAB[0], "allo ");
strcpy(TAB[1], "les amis ");
realloc2d(TAB, 2, 25); // Si la la nouvelle taille est plus petite que l'ancienne, il peut y avoir des problèmes
strcat(TAB[1], "sa va ?");
printf("%s%s\n", TAB[0], TAB[1]);
free2d(TAB, 2);
Galmiza >> Tu es sûr? Il s'agit pourtant bien d'allocation dynamique.
C++ (@++)