c4-pointers and dynamic memory allocation

7
Pointers & Dynamic Memory Allocation Mugurel Ionuț Andreica Spring 2012

Upload: leslie-nelson

Post on 09-Sep-2015

219 views

Category:

Documents


3 download

DESCRIPTION

poi

TRANSCRIPT

  • Pointers & Dynamic Memory AllocationMugurel Ionu Andreica

    Spring 2012

  • The Structure of a Programs Memory SpaceMemory space of a program=a sequence of bytesThe bytes are numbered starting from 0: 0, 1, 2, ...Every variable occupies a certain number of consecutive bytes in memoryAn int occupies 4 bytesA double occupies 8 bytesA char occupies 1 byteThe address of a variable = the index of the first byte occupied in memoryIf an int variable occupies the bytes numbered 1000, 1001, 1002, 1003, the address of the variable is 1000

  • Memory Content & PointersEvery variable stores its value in the bytes it occupies in memoryA char stores its value in 1 byteAn int stores its value as 4 consecutive bytesPointer = special type of variableIts 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 storesMultiple types of pointersint* 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 addressDifferences: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

    Text

  • Dynamic Memory AllocationWe 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 pointernew type_name => allocates a memory zone to store a value of the type type_name and returns its addressnew 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 addraddr must be the address of a memory zone which was previously dynamically allocated

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

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