Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointers & Dynamic Memory Allocation Mugurel Ionu Andreica Spring 2012.

Similar presentations


Presentation on theme: "Pointers & Dynamic Memory Allocation Mugurel Ionu Andreica Spring 2012."— Presentation transcript:

1 Pointers & Dynamic Memory Allocation Mugurel Ionu Andreica Spring 2012

2 The Structure of a Program’s 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

3 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)

4 Pointers - example

5 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

6 Pointers & Dynamic Memory Allocation – Sample Program #include 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);

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


Download ppt "Pointers & Dynamic Memory Allocation Mugurel Ionu Andreica Spring 2012."

Similar presentations


Ads by Google