Upload
barthomieu-besson
View
103
Download
0
Embed Size (px)
Citation preview
1
2
3
4
5
La fonction alloue un bloc de taille size.
Il faut indiquer la taille du bloc que l’on veut allouer.
Le premier exemple:
#include <stdio.h>#include <stdlib.h>void main() { int * p;
p = (int *)malloc (sizeof(int));if ( p == NULL )
{ fprintf(stderr,"Allocation impossible \n"); exit(EXIT_FAILURE);
} }
Message d’erreur
Allocation pour un nombre entier
Le contenu de la zone allouée est aléatoire .
La fonction alloue un bloc de taille size.
6
Allocation pour un tableau de 3 nombres réels
#include <stdio.h>#include <stdlib.h>void main() { float * tab;
tab = (float *)malloc (3*sizeof(float));if ( tab == NULL )
{ fprintf(stderr,"Allocation impossible \n"); exit(EXIT_FAILURE);
} }
Allocation pour un tableau
7
8
9
10
11
L’ajout d’un élément nouveau
12
L’ajout d’un élément nouveau
13
L’ajout d’un élément nouveau
L’ancien tableau n’existe plus
14
L’adresse du nouveau tableau
15
16
La fonction ne fait qu’un changement de taille.
On peut ajouter ou enlever une case à la fin d’un tableau dynamique sans le modifier .
#include <stdio.h> 1/2#include <stdlib.h>void main() { int * tab,i;
tab = (int *)calloc (3 , sizeof(int));if ( tab == NULL )
{ fprintf(stderr,"Allocation impossible \n"); exit(EXIT_FAILURE);
}tab[0]=1;tab[1]=2;tab[2]=3;
Création d’un tableau de 3 entiers
Initialisation
17
tab=(int *)realloc(tab, 4 * sizeof(int)); 2/2tab[3]=4;
for(i=0;i<4;i++)printf(“tab[%d]=%d\n”,i,tab[i]);
}
Ajout d’un élément au tableau
tab[0]=1tab[1]=2tab[2]=3tab[3]=4
Initialisation
Affichage du tableau
18
19
Si vous tentez de libérer un pointeur NULL la fonction ne fera strictement rien.
Si vous libérer un bloc qui a précédement été désalloué, le comportement de la fonction est alors indéterminé.
Il faut forcer le pointeur que l'on vient de libérer à la valeur NULL .
. . .int * entier= (int *)malloc (sizeof(int));if( entier == NULL )
{ fprintf(stderr,"Allocation impossible");}else
{ *entier = 3; printf("%d",*entier); free(entier); entier = NULL;}
libération
20
#include <stdio.h> 1/2#include <conio.h> #include <stdlib.h>void main()
{ int *sptr,*eptr,*current,*nouv; sptr=(int*)calloc(7,sizeof(int)); eptr=sptr+7; printf("Avant realloc\n"); for(current=sptr;current<eptr;current++)
{ *current=rand();printf("%d\n",*current);
}
Exemple 1Réserver de la mémoire pour 7 éléments entiers. Remplir la zone avec nombres arbitraires et les afficher. Modifier la taille de la mémoire préalablement alloué et afficher la nouvelle zone.
Avant realloc34613010982109011656711717595
Allocation
Remplissage et affichage
21
free(sptr); 2/2 printf("Appuyer une touche!\n"); getch(); nouv=(int*)realloc(sptr,20); sptr=(int *)nouv; eptr=sptr+12; printf("Apres realloc\n"); for(current=sptr;current<eptr;current++)
printf("%d\n",*current); free(sptr);}
Exemple 1
Appuyer une touche!Apres realloc34613010982109011656711717595-143609175320040
Modification de la taille
Affichage de la nouvelle zone
22
#include <stdio.h> 1/2#include <stdlib.h>void main()
{ int *sptr,*eptr,*current,*nouv,*min_ptr; int min; sptr=(int*)calloc(7,sizeof(int)); eptr=sptr+7; for(current=sptr;current<eptr;current++)
*current=rand(); min=*sptr; for(current=(sptr+1);current<eptr;current++)
if(*current<min){min=*current; min_ptr=current;}
Exemple 2Faire les même réservations de la mémoire. Trouver l’ élément plus petit. Voir comment se déplace le pointeur.
Allocation et remplissage
L’ élément minimal
23
printf("Avant realloc:min=%d\n",*min_ptr); 2/2 free (sptr); nouv=(int*)realloc(sptr,5); min_ptr+=nouv-sptr; printf("Apres realloc:min=%d\n",*min_ptr);}
Avant realloc:min=130Apres realloc:min=-1
Exemple 2Faire les même réservations de la mémoire. Trouver l’ élément plus petit. Voir comment se déplace le pointeur.
déplacement du pointeur