Documente Academic
Documente Profesional
Documente Cultură
Plan
1 2 3
Programmation C - ENSTA - 2
Mmoire
0x0000 0x7F00 0x0800 0x0900 0x0A00 0x0B00 0x0C00 0x0D00 0xFF00
Pointeur
Systme
0x0800 0x0900 0x0A00 0x0B00
a b
Programme
0x0C00
Systme
int a;
David Pointcheval LIENS - CNRS
int a; 0x0902 pointeur sur (int) a short b; 0x0A0D pointeur sur (short) b
David Pointcheval LIENS - CNRS
Une variable est stocke dans une zone mmoire rserve lors de la dclaration & oprateur de rfrencement (adresse)
Programmation C - ENSTA - 4
Pointeurs = Adresse
& &a
&c
ad re ss es
Oprateur de contenu
pa pointeur sur un entier int *pa; * oprateur de drfrencement (contenu) *pa est la valeur contenue ladresse pa
&b
oprateur dadresse
(de rfrencement)
&b &a
* & rciproques
pa
a
David Pointcheval LIENS - CNRS
b c
Programmation C - ENSTA - 5 David Pointcheval LIENS - CNRS
*pa b c
Programmation C - ENSTA - 6
Quelques exemples
a c 5 2.5 pf0x0A01 float c,*pf; pf = &c; c = 5; *pf = *pf / 2; 30 10 b 30 pi 0x0802
int *pa; pa pointeur sur un int float *pb; pb pointeur sur un float char *pc; pc pointeur sur un char
David Pointcheval LIENS - CNRS
Programmation C - ENSTA - 7
Programmation C - ENSTA - 8
Allocation dynamique
int *pa; dclaration dun pointeur pa sur un int rservation dune zone mmoire pour stocker une adresse pa pas dinitialisation pas dentier point il faut rserver la zone mmoire pour cet entier : allocation ? puis affecter le pointeur
David Pointcheval LIENS - CNRS Programmation C - ENSTA - 10
Mais tout pointeur est une adresse mmoire, soit 32 bits (GCC Linux) dans la plupart des cas, tous les pointeurs seront quivalents
David Pointcheval LIENS - CNRS Programmation C - ENSTA - 9
Utilisation de malloc
int *pa; pa = malloc(sizeof(int));
pa
pa = malloc(sizeof(int));
0x0A00 0x0B00 0x0C00
David Pointcheval LIENS - CNRS
pa0x0A06
Programmation C - ENSTA - 11
void *malloc(int n) allocation de n octets de mmoire, ladresse de cette zone est retourne pointeur gnrique, void * il est automatiquement cast en pointeur typ (ds que possible)
David Pointcheval LIENS - CNRS Programmation C - ENSTA - 12
pa
pa = malloc(3*(sizeof(int)));
0x0A00 0x0B00 0x0C00
David Pointcheval LIENS - CNRS
pa0x0A01
Programmation C - ENSTA - 14
Tableaux dynamiques
int t[3]; tableau de 3 entiers
Tf = malloc(6*(sizeof(float)));
t 0x0A00
t[0]
t[1]
t[2]
quivalent
int *t = malloc(3*(sizeof(int)));
Tf 0x0A00
Programmation C - ENSTA - 16
Programmation C - ENSTA - 15
Tf = malloc(6*(sizeof(float)));
0x0A00 0x0B00 0x0C00 0x0D00 0x0E00 0x0F00
10.5
2.1 1.1
0.0
Tf 0x0A00
Programmation C - ENSTA - 18
Tableaux et pointeurs
float *Tf;
Tf = malloc(6*(sizeof(float)));
0x0A00 0x0B00 0x0C00 0x0D00 0x0E00 0x0F00
10.5
2.1 1.1
Tf 0x0A00
T
T[0][0]
David Pointcheval LIENS - CNRS
T[0]
T[0][n-1]
T[m-1]
T[m-1][0]
T[m-1][n-1]
Programmation C - ENSTA - 20
T 0x0A01
Programmation C - ENSTA - 21
T[0][0] = l; T[0][1] = a; T[0][2] = \n; T[1][0] = i; T[1][1] = c; T[1][2] = i; T[2][0] = a; T[2][1] = \n;
David Pointcheval LIENS - CNRS Programmation C - ENSTA - 22
Allocation permanente
#define N 10 int *initialisation() { int T[N]; int i; for (i=0; i<N; i++) T[i] = 0; return T; }
Conclusion
Pointeur Allocation dynamique allocation de mmoire pour des pointeurs tableaux de taille variable tableaux plusieurs dimensions libration dynamique
David Pointcheval LIENS - CNRS Programmation C - ENSTA - 25