Sunteți pe pagina 1din 7

Programmation C

Pointeurs et allocation de mmoire


David Pointcheval

Plan

1 2 3

- Pointeurs - Allocation dynamique - Tableaux plusieurs dimensions

E-mail : David.Pointcheval@ens.fr Web : http://www.di.ens.fr/~pointche/enseignement/ensta

David Pointcheval LIENS - CNRS

Programmation C - ENSTA - 2

Mmoire
0x0000 0x7F00 0x0800 0x0900 0x0A00 0x0B00 0x0C00 0x0D00 0xFF00

Pointeur
Systme
0x0800 0x0900 0x0A00 0x0B00

a b

&a : 0x0902 &b : 0x0A0D

Programme

0x0C00

Systme

int a;
David Pointcheval LIENS - CNRS

short b; b en 0x0A0D a en 0x0902


Programmation C - ENSTA - 3

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)

fournit ladresse mmoire de la variable a


int a,b; long long c;

&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

Pointeurs de tous types


0x0800 0x0900

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

0x0A00 0x0B00 0x0C00

int a,b,*pi; pi = &a; a = 10; b = 30; *pi = b;

Programmation C - ENSTA - 7

David Pointcheval LIENS - CNRS

Programmation C - ENSTA - 8

Taille dun pointeur


En gnral, les pointeurs sont typs :
int *pa; float *pb;

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

pointeur sur un int pointeur sur un float

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

Allocation dynamique : malloc


int *pa;
0x0A00 0x0B00 0x0C00

Utilisation de malloc
int *pa; pa = malloc(sizeof(int));

pa

dclaration dun pointeur pa sur un int


a pa0x0A06 int a; pa = &a;

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

Taille dun objet sizeof


La taille dun type nest pas standardise pour la portabilit dun programme C, utiliser sizeof(<type>) GCC Linux: sizeof(int) 4 sizeof(char) 1
David Pointcheval LIENS - CNRS Programmation C - ENSTA - 13

Allocation dynamique : tableaux


int *pa;
0x0A00 0x0B00 0x0C00

pa

dclaration dun pointeur pa sur un int


a pa0x0A06 int a; pa = &a;

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

Tableaux dynamiques (suite)


float *Tf;
dclaration dun pointeur Tf sur un float

Tf = malloc(6*(sizeof(float)));
t 0x0A00

t[0]

t[1]

t[2]

0x0B00 0x0C00 0x0D00 0x0E00 0x0F00


David Pointcheval LIENS - CNRS

quivalent
int *t = malloc(3*(sizeof(int)));

Tf 0x0A00
Programmation C - ENSTA - 16

David Pointcheval LIENS - CNRS

Programmation C - ENSTA - 15

Tableaux dynamiques (suite)


float *Tf;

Tableaux dynamiques (suite)


Ainsi,
<type> *t = malloc(k*(sizeof(<type>))); dfinit un tableau de k objets de type <type>, o k peut tre une variable : taille non dfinie la compilation <type> peut tre un tableau (pointeur) : tableau plusieurs dimensions

Tf = malloc(6*(sizeof(float)));
0x0A00 0x0B00 0x0C00 0x0D00 0x0E00 0x0F00

10.5

2.1 1.1

0.0

Tf 0x0A00

Tf[0] = 10.5; Tf[3] = 0;


David Pointcheval LIENS - CNRS

Tf[1] = 2.1; Tf[5] = 1.1;


Programmation C - ENSTA - 17

David Pointcheval LIENS - CNRS

Programmation C - ENSTA - 18

Tableaux et pointeurs
float *Tf;

Tableaux plusieurs dimensions


int i, m = 10, n = 15; int **T; T = malloc(m * sizeof(int *)); for (i=0; i<m; i++) T[i] = malloc(n * sizeof(int));

Tf = malloc(6*(sizeof(float)));
0x0A00 0x0B00 0x0C00 0x0D00 0x0E00 0x0F00

10.5

2.1 1.1

0.0 Tableau de float

Tf 0x0A00

Tf : Tf+1: Tf+3: Tf+5:

0x0A00 0x0A04 0x0A0C 0x0B04

T est un pointeur de pointeur dentier = tableau de tableau dentiers

T
T[0][0]
David Pointcheval LIENS - CNRS

*Tf = 10.5; *(Tf+3)= 0;


David Pointcheval LIENS - CNRS

*(Tf+1) = 2.1; *(Tf+5) = 1.1;


Programmation C - ENSTA - 19

T[0]

T[0][n-1]

T[m-1]
T[m-1][0]

T[m-1][n-1]
Programmation C - ENSTA - 20

Plusieurs dimensions (suite)


int i, m = 3, n = 5; char **T; T = malloc(m * sizeof(char *)); for (i=0; i<m; i++) T[i] = malloc(n * sizeof(char));

Plusieurs dimensions (suite)


int i, m = 3, n = 5; char **T = malloc(m * sizeof(char *)); for (i=0; i<m; i++) T[i] = malloc(n * sizeof(char)); 0x0A00 0x0B00 0x0C00 0x0D00 0x0E00 0x0F00

0x0D02 0x0E09 0x0C0A a \n l a \n T 0x0A01 ic i

0x0A00 0x0B00 0x0C00 0x0D00 0x0E00 0x0F00


David Pointcheval LIENS - CNRS

0x0D02 0x0E09 0x0C0A

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; }

Libration de mmoire : free


De la mmoire alloue avec malloc ne sera libre qu la fin du programme On peut cependant vouloir librer cette mmoire pour un autre usage (la mmoire de lordinateur nest pas illimite) commande free
int *pa = malloc((sizeof(int)); /*allocation*/ free(pa); /*libration*/
David Pointcheval LIENS - CNRS Programmation C - ENSTA - 24

Tableau T local la fonction, libr la fin


int *initialisation(int n) { int i; int *T = malloc(n * sizeof(int)); for (i=0; i<n; i++) T[i] = 0; return T; }
David Pointcheval LIENS - CNRS Programmation C - ENSTA - 23

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

S-ar putea să vă placă și