Presentation is loading. Please wait.

Presentation is loading. Please wait.

Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.

Similar presentations


Presentation on theme: "Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding."— Presentation transcript:

1 Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding Memory Leaks PROGRAMMING FUNDAMENTALS

2 INTRODUCTION So far, fixed amount of memory is used Program could not increase or decreased memory during runtime Dynamic memory allow you to create and use data structure that can grow and shrink.

3 Static and Dynamic Memory With static memory, you define the maximum amount of space required. –char a[1000]; There no way to change amount of memory in runtime. Dynamic memory comes in blocks without names, just address.

4 Static and Dynamic Memory (cont.) malloc() function is used to request a block of certain size (in byte). int memsize; // memsize get a value at runtime x=malloc(memsize);

5 Static and Dynamic Memory (cont.) free() function is used to return it (memory) to the pool. free(x); Every call to malloc() must be balanced by a call to free().

6 Static and Dynamic Memory (cont.) malloc() return the starting address of the newly allocated block. The starting address to be stored in pointer of type void; Void *x; A void pointer variable can store address of any type. Refer to figure 10.1 Refer to script 10.1

7 Script 10.1 #include #include // required by malloc() and free() int main(void) { void *x;/* declare a void pointer variable */ x=malloc(1000); /* request a block of memory with 1000 byte */ cout<<"starting block address :"<

8 Figure 10.1 xmemsize

9 Static and Dynamic Memory (cont.) Static and Dynamic Memory (cont.) Void *x; x= malloc(size); Free(x); x=NULL; The pointer should be set to NULL after freed.

10 TYPE CAST TYPE CAST The void pointer variable allows you to store and retrieve any kind of address (integer, float, double, string etc). Since the pointer can point to anything, the system cannot know if it should read or write a 1-byte or a 4-byte integer, or something completely different.

11 TYPE CAST (cont.) TYPE CAST (cont.) System need to be informed- what kind of data is stored. Void *x; (int *)x; It is called type-cast. Script 10.2

12 /* typecast.c - Script 10.2 */ #include int main (void) { void *x = NULL; /* Define and initialize a void pointer variable. */ x = malloc(sizeof(int)); /* Request a block of memory large enough to hold an integer. */ /* We have omitted the check for NULL here. You must always check for a NULL return value from malloc(). */ /* Print out the starting address of the memory block. */ cout<<"Starting address: “<< x<

13 /* Store an integer in the new memory block. */ *(int *)x = 1234; /* Print the value. */ cout<<"Value stored in the memory block: “<< *(int*)x); /* Return the block to the pool. */ free(x); x = NULL; /* Pause and wait for input before terminating. */ getchar(); return 0; }

14 ALLOCATING ARRAYS OF DYNAMIC SIZE ALLOCATING ARRAYS OF DYNAMIC SIZE The example discussed in previous still consider static memory. The amount of memory should be determine and request during runtime. Refer to script 10.3

15 /* array.c - Script 10.3 */ #include int main (void) { /* Define an integer pointer variable. */ int *x = NULL; /* Define two integer variables. */ int i, count; /* Prompt for the number of items. Store the user's reply in the count variable. */ cout<<"Number of items? "; cin>>count;

16 /* Request a block of memory large enough to hold the requested number of integers. */ x = (int*)malloc(count * sizeof(int)); /* We have omitted the check for NULL here. You must always check for a NULL return value from malloc(). Store a random number in every slot of the array. */ for (i = 0; i < count; i++) { x[i] = rand(); }

17 /* Print all values in the array. */ for (i = 0; i < count; i++) { cout<<“The value of array element “<< i <<“ is “<

18 Resizing a Block of Memory Sometime you have to resize a block of dynamic memory after you have already stored data in it. The C++ library provides the realloc() function for this purpose. void *x *y; x=malloc(1000); // initial block Y= realloc(x,2000);// doubled

19 Resizing a Block of Memory(2) realloc() attempts to grow the original size to accommodate the new requested size. If successful:- it returns the block’s unchanged. If it not possible to grow the old block (not enough adjacent free space in memory):- realloc() will instead allocate a new block in a different part of memory; copy all over of the old data to the new block and return the address of that new block.

20 Resizing a Block of Memory(3) Resizing a Block of Memory(3) Upon failure, the realloc() returns the NULL pointer instead.

21 Stop! Do exercise This code is bad, why? void *x ; x=malloc(1000); // initial block x= realloc(x,2000);// doubled If realloc() fails and return a NULL pointer, the initials address of the memory block returned by the malloc(1000) call is lost. Since that value can longer be passed to free(), the originally reserved memory cannot be reclaimed until the program terminates. This is called memory leak.

22 Summary Dynamic memory allow you to create and use data structure that can grow and shrink. malloc() function is used to request a block of certain size (in byte). free() function is used to return it (memory) to the pool.

23 Summary(2) realloc adjusts the size of the allocated block to size, copying the contents to a new location if necessary.


Download ppt "Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding."

Similar presentations


Ads by Google