La fonction alloue un bloc de taille size' - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

La fonction alloue un bloc de taille size'

Description:

Le contenu de la zone allou e est al atoire . La fonction alloue un bloc de taille size. ... Remplir la zone avec nombres arbitraires et les afficher. Modifier la taille de la ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 24
Provided by: julianag7
Category:

less

Transcript and Presenter's Notes

Title: La fonction alloue un bloc de taille size'


1
(No Transcript)
2
(No Transcript)
3
(No Transcript)
4
(No Transcript)
5
La fonction alloue un bloc de taille size.
La fonction alloue un bloc de taille size.
Il faut indiquer la taille du bloc que lon veut
allouer.
Le contenu de la zone allouée est aléatoire .
Le premier exemple
include ltstdio.hgt include ltstdlib.hgt void
main() int p p (int )malloc
(sizeof(int)) if ( p NULL )
fprintf(stderr,"Allocation impossible \n")
exit(EXIT_FAILURE)
Allocation pour un nombre entier
Message derreur
6
Allocation pour un tableau de 3 nombres réels
include ltstdio.hgt include ltstdlib.hgt void
main() float tab tab (float )malloc
(3sizeof(float)) if ( tab NULL )
fprintf(stderr,"Allocation impossible \n")
exit(EXIT_FAILURE)
Allocation pour un tableau
7
(No Transcript)
8
(No Transcript)
9
(No Transcript)
10
(No Transcript)
11
Lajout dun élément nouveau
12
Lajout dun élément nouveau
13
Lajout dun élément nouveau
Lancien tableau nexiste plus
14
Ladresse du nouveau tableau
15
(No Transcript)
16
La fonction ne fait quun changement de taille.
On peut ajouter ou enlever une case à la fin dun
tableau dynamique sans le modifier .
include ltstdio.hgt 1/2 include
ltstdlib.hgt void main() int tab,i tab
(int )calloc (3 , sizeof(int)) if ( tab
NULL ) fprintf(stderr,"Allocation
impossible \n") exit(EXIT_FAILURE)
tab01 tab12 tab23
Création dun tableau de 3 entiers
Initialisation
17
Ajout dun élément au tableau
tab(int )realloc(tab, 4 sizeof(int))
2/2 tab34 for(i0ilt4i) printf(
tabdd\n,i,tabi)
Initialisation
Affichage du tableau
tab01 tab12 tab23 tab34
18
(No Transcript)
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
Exemple 1 Ré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.
Allocation
include ltstdio.hgt 1/2 include
ltconio.hgt include ltstdlib.hgt void
main() int sptr,eptr,current,nouv
sptr(int)calloc(7,sizeof(int))
eptrsptr7 printf("Avant realloc\n")
for(currentsptrcurrentlteptrcurrent) curr
entrand() printf("d\n",current)
Avant realloc 346 130 10982 1090 11656 7117 17595
Remplissage et affichage
21
Exemple 1
Modification de la taille
free(sptr) 2/2
printf("Appuyer une touche!\n") getch()
nouv(int)realloc(sptr,20) sptr(int
)nouv eptrsptr12 printf("Apres
realloc\n") for(currentsptrcurrentlteptrcurr
ent) printf("d\n",current)
free(sptr)
Appuyer une touche! Apres realloc 346 130 10982 10
90 11656 7117 17595 -1 4360 9175 32004 0
Affichage de la nouvelle zone
22
Exemple 2 Faire les même réservations de la
mémoire. Trouver l élément plus petit. Voir
comment se déplace le pointeur.
include ltstdio.hgt 1/2 include
ltstdlib.hgt void main() int sptr,eptr,current
,nouv,min_ptr int min
sptr(int)calloc(7,sizeof(int))
eptrsptr7 for(currentsptrcurrentlteptrcurr
ent) currentrand() minsptr
for(current(sptr1)currentlteptrcurrent) if
(currentltmin) mincurrent
min_ptrcurrent
Allocation et remplissage
L élément minimal
23
Exemple 2 Faire les même réservations de la
mémoire. Trouver l élément plus petit. Voir
comment se déplace le pointeur.
printf("Avant reallocmind\n",min_ptr)
2/2 free (sptr) nouv(int)realloc(s
ptr,5) min_ptrnouv-sptr
printf("Apres reallocmind\n",min_ptr)
déplacement du pointeur
Avant reallocmin130 Apres reallocmin-1
Write a Comment
User Comments (0)
About PowerShow.com