Vous êtes sur la page 1sur 7

Pointers & Dynamic Memory Allocation

Mugurel Ionu Andreica


Spring 2012

The Structure of a Programs Memory Space


Memory space of a program=a sequence of bytes
The bytes are numbered starting from 0: 0, 1, 2, ...

Every variable occupies a certain number of consecutive bytes in memory


An int occupies 4 bytes A double occupies 8 bytes A char occupies 1 byte

The address of a variable = the index of the first byte occupied in memory
If an int variable occupies the bytes numbered 1000, 1001, 1002, 1003, the address of the variable is 1000

Memory Content & Pointers


Every variable stores its value in the bytes it occupies in memory
A char stores its value in 1 byte An int stores its value as 4 consecutive bytes

Pointer = special type of variable


Its size = architecture-dependent (32 bits = 4 bytes in our case) Stores an address (the index of a byte in memory) The pointer points to the address it stores Multiple types of pointers
int* p (pointer to int) char* p (pointer to char) int** p (pointer to pointer to int) double*** p ... Each type of pointer has 4 bytes and stores an address Differences:
int x; int *p, char* q; p = q = &x; *p // refers to the int which starts at the address &x *q // refers to the char located at the address &x (the first byte of the 4-byte int variable x)

Pointers - example
*p *q

1000 1001 1002 1003

2000

3000 3001 3002 3003

4000 4001 4002 4003

...

int x or *p or **r

...

char y

...

int* p = 1000

...

char* q = 2000

*s 5000 5001 5002 5003

*r 6000 6001 6002 6003

7000 7001 7002 7003

...

int** r = 3000

...

char* s = 1000 *t

...

char** t = 6000

Dynamic Memory Allocation


We can assign to a pointer the address of an existing variable (or the address stored by another pointer) We can allocate memory on demand (dynamically) and assign the address of the allocated memory zone to a pointer new type_name => allocates a memory zone to store a value of the type type_name and returns its address new type_name[20] => allocates a contiguous memory zone to store 20 consecutive values of the type type_name and returns the address of the first such value (which is the address of the memory zone) Internally, the operating system stores the size of a dynamically allocated memory zone (how many bytes were allocated starting from the address of the memory zone) delete pointer to addr => deallocates the memory zone whose address is addr
addr must be the address of a memory zone which was previously dynamically allocated

Pointers & Dynamic Memory Allocation Sample Program


#include <stdio.h> #include <stdlib.h> struct mystruct { int x; char b[20]; double d; struct mystruct *next; }; int x, *p1, **pp1, ***ppp1; int *v, **A; struct mystruct *sp, **spp, ***sppp, *vs, *e1, *e2, *e3, *e; int main() { x = 7; p1 = new int; *p1 = 10; printf("%d, %d\n", p1, *p1); p1 = &x; printf("%d, %d\n", p1, *p1); pp1 = new int*; *pp1 = new int; **pp1 = 11; printf("%d, %d, %d\n", pp1, *pp1, **pp1); *pp1 = p1; printf("%d, %d, %d\n", pp1, *pp1, **pp1); pp1 = &p1; printf("%d, %d, %d\n", pp1, *pp1, **pp1); x = 3; printf("%d, %d, %d\n", pp1, *pp1, **pp1); **pp1 = 5; printf("%d\n", x); ppp1 = &pp1; printf("%d, %d, %d, %d\n", ppp1, *ppp1, **ppp1, ***ppp1); ***ppp1 = 90; printf("%d\n", x); ppp1 = new int**; *ppp1 = new int*; **ppp1 = new int; ***ppp1 = 9; printf("%d, %d, %d, %d\n", ppp1, *ppp1, **ppp1, ***ppp1); printf("%d\n", x);

Pointers & Dynamic Memory Allocation Sample Program (cont.)


v = new int[100]; v[x = 17] = 8; printf("%d\n", v[x]); A = new int*[600]; A[345] = new int[512]; A[345][100] = 16; printf("%d, %d\n", A[345][100], A[345][101]); vs = new struct mystruct[100]; for (int i = 0; i < 50; i++) vs[i].x = 18; printf("%d, %d\n", vs[33].x, vs[66].x); delete vs; printf("%d, %d\n", vs[33].x, vs[66].x); e1 = new struct mystruct; e1->x = 8; e2 = new struct mystruct; e2->x = 19; e1->next = e2; e3 = new struct mystruct; e3->x = 23; e2->next = e3; e3->next = NULL; e = e1; while (e != NULL) { printf("e->x=%d\n", e->x); e = e->next; } return 0; }

sp = new struct mystruct; sp->x = 17; sp->b[3] = 14; sp->d = 9.3; printf("%d, %d\n", sp->x, (*sp).x); spp = &sp; printf("%d, %d\n", (*spp)->x, (**spp).x); sppp = &spp; (***sppp).x = 9; printf("%d, %d, %d, %d\n", (***sppp).x, (**sppp)->x, sp->x, (*spp)->x); delete sp; printf("%d, %d, %d, %d\n", (***sppp).x, (**sppp)->x, sp->x, (*spp)->x);

Vous aimerez peut-être aussi