Bon si tu y tiens ...
void *realloc(void *ptr, size_t size);
realloc() changes the size of the memory block pointed to by ptr to size bytes. The contents will be unchanged to the minimum of the old and new sizes; newly allocated memory will be uninitial ized. If ptr is NULL, the call is equivalent to malloc(size); if size is equal to zero, the call is equivalent to free(ptr). Unless ptr is NULL, it must have been returned by an earlier call to malloc(), calloc() or realloc().
realloc() returns a pointer to the newly allocated memory, which is suitably aligned for any kind of variable and may be different from ptr, or NULL if the request fails or if size was equal to 0. If realloc() fails the original block is left untouched - it is not freed or moved.
Code :
- celluleTab tab;
- cellule* nouveau_tableau=NULL;
- /* Initialisation */
- memset(&tab, 0, sizeof(celluleTab));
- [...]
- /* Agrandissement */
- nouveau_tableau=(cellule*)realloc(tab.celtab, sizeof(cellule) * nb_cellules_nouveau_tableau);
- if (nouveau_tableau == NULL)
- {
- /* Message d'erreur, blabla */
- }
- else
- {
- tab.celtab=nouveau_tableau;
- }
- [...]
- /* Liberation */
- if (tab.celtab != NULL) free(tab.celtab);
- tab.celtab=NULL;
- [...]
|
Voilà un exemple qui doit fonctionner.
Pour plus de clarté tu pourrais utiliser des noms de structures en majuscule par exemple.
Message édité par darkoli le 01-02-2004 à 12:39:22