Presentation is loading. Please wait.

Presentation is loading. Please wait.

Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College

Similar presentations


Presentation on theme: "Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College"— Presentation transcript:

1 Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College nazri@tatiuc.edu.my

2 2.1 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 2.2 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 2.2 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 2.2 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 2.2 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 Figure 10.1 xmemsize 2412 012345678910111213141516171819 2021222324252627282930313233343536373839

8 /* memory.c - Script 10.1 */ #include int main (void) {/* Declare a void pointer variable. */ void *x; /* Request a block of memory with 1000 bytes. */ x = malloc (1000); /* Print out the starting address of the memory block. */ cout<<"Starting block address: “<< x; /* Return the block to the pool. */ free(x); x = NULL; /*Pause and wait for input before terminating. */ //getchar(); return 0; }

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

10 2.3 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 2.3 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) { /* Define and initialize a void pointer variable. */ void *x = NULL; /* Request a block of memory large * enough to hold an integer. */ x = malloc(sizeof(int)); /* 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. */

13 cout<<"Starting address: “<< x<<endl; /* 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 2.4 ALLOCATING ARRAYS OF DYNAMIC SIZE The example discussed in previous still consider static memory. The amount of 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 “<<x[i] <<endl; } /* Return the block to the pool. */ free(x); x = NULL; /* Pause and wait for input before terminating. */ return 0; }

18 2.5 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 2.5 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 2.5 Resizing a Block of Memory(3) Upon failure, the realloc() returns the NULL pointer instead. Script 10.4

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 DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College"

Similar presentations


Ads by Google