Presentation is loading. Please wait.

Presentation is loading. Please wait.

DYNAMIC MEMORY MANAGEMENT. int x; When the source code containing this statement is compiled and linked, an executable file is generated. When the executable.

Similar presentations


Presentation on theme: "DYNAMIC MEMORY MANAGEMENT. int x; When the source code containing this statement is compiled and linked, an executable file is generated. When the executable."— Presentation transcript:

1 DYNAMIC MEMORY MANAGEMENT

2 int x; When the source code containing this statement is compiled and linked, an executable file is generated. When the executable file is executed, instruction for allocating the memory for ‘x’ is executed and memory gets allocated for ‘x’. This is known as ‘static memory allocation’ although memory gets allocated at run time. During execution, the instruction to deallocate memory for ‘x’ is executed and memory gets deallocated (static deallocation) The programmers are forced to predict the total amount of data the program will utilize. Statements are written to declare pre-calculated amount of memory. During run time, if memory is required, static memory allocation can not fulfill the need.

3 Memory allocated cannot be released immediately, although it is not used. It will be held up until the end of block execution Dynamic memory management overcomes the drawback of static memory allocation. Memory gets allocated and deallocated during runtime only. The decisions to do so can be taken dynamically in response to the requirements arising during runtime itself Dynamic memory allocation It is achieved in C through malloc(), calloc() and realloc() functions In C++ it is achieved through ‘new’ operator

4 #include void main() { int * iPtr; iPtr= new int ; *iPtr = 10; cout << *iPtr << endl; } // end of dynamic.cpp Output : 10

5 ‘new’ takes a predefined data type as an operand. It then allocates memory to hold one value of the data type that is passed as a parameter in the heap. Finally it returns the address of the allocated block(need not be type casted). This address can be stored in a pointer and can be accessed through it The general syntax is = new The new operator can be used to create multiple blocks of memory also = new [ ]

6 #include # define SIZE 10 void main() { int * iPtr; iPtr= new int [SIZE] ; for ( int i = 0 ; i < SIZE ; i++) iPtr [i] = i ; for ( int i = 0 ; i < SIZE ; i++) cout << iPtr [i] << endl ; } // end of dynarr1.cpp Output: 0 to 9 in subsequent lines

7 #include //size specified at runtime void main() { int * iPtr; unsigned int isize ; cout <<“Enter the size of the array: “; cin >> isize ; iPtr= new int [isize] ; for ( int i = 0 ; i < isize ; i++) { cout << “Enter the value for element “<<i+1<<“:“; cin >> iPtr [i] ; } for ( int i = 0 ; i < SIZE ; i++) cout << iPtr [i] << endl ; } // end of dynarr2.cpp

8 #include //array of objects #include”Distance.h” void main() { Distance * dPtr; int a; float b; unsigned int isize ; cout <<“Enter the number of the elements: “; cin >> isize ; dPtr= new Distance [isize] ; for ( int i = 0 ; i < isize ; i++) { cout << “Enter the feet : “ ; cin >> a ;

9 cout << “Enter the inches : “ ; cin >> b ; dPtr[i]. setFeet(a); dPtr[i]. Setnches(b); } for ( int i = 0 ; i < SIZE ; i++) cout << dPtr [i].getFeet() << “ ” << dPtr[i].getInches()<< endl ; } // end of dyndist.cpp The amount of memory to be allocated is being decided during runtime itself.

10 Dynamic memory deallocation Once the allocated memory is not in use any more, it should be returned to the OS It is achieved through the free() function in C In C++, through the delete operator General syntax – delete The memory block will be deallocated only at the end of the functions that allocates it. However, it is usually conditional Misconception about delete – the memory being occupied by the pointer itself gets removed if the delete operator is used on the pointer

11 #include void abc(); void main() { abc(); // rest of main() function } void abc() {int *iPtr; iPtr = new int; //rest of abc() function } // end of memleak.cpp

12 When abc() executes, memory will be allocated and base address is returned which is stored in pointer When abc() finishes execution, ‘iPtr’(pointer) itself is deallocated, but the memory in the heap area remains locked up as an orphan. This is known as memory leak. This block of memory can not be accessed since the only pointer was removed from the stack It can be deallocated by passing the pointer pointing to it as an operand to the delete operator delete iPtr; is executed just before the abc() function terminates and memory leak is prevented

13 When the delete operator is used on the pointer that points at this block of memory, it gets deallocated and made available for the OS The block of memory locked up by the code in a certain function persists even after the function terminates is frequently desirable and can have a reference void abc ( int ** p) { // some complex algorithm *p = new int ; // rest of abc() function }

14 void main () { int * iPtr ; abc (& iPtr ) // rest of main function } A block of memory is allocated and its address is stored in ‘iPtr’ by dereferencing the pointer that points at it After abc() terminates, ‘iPtr’ that is a local variable in the calling function will point at the dynamically allocated block of memory

15 void abc ( int ** p) { if (memory_not_required) { delete * p; *p = null ; } // rest of abc() function } When the delete operator is used on a pointer, the pointer continues to occupy its own block of memory and continues to have the same value, the address of the memory that has just get deallocated. This will lead to runtime errors if the pointer is dereferenced

16 The pointer being pointed at by ‘p’ was deliberately nullified after the memory has been deallocated It is highly desirable that either the pointer points at a valid dynamically allocated block or be NULL. But it is not possible to ensure this There is no guaranteed initialization of data A multiple block of memory is deallocated by suffixing delete operator with [] followed by the pointer - delete[] ; Distance * dPtr ; dPtr = new Distance[5] ;//creates an array delete [] dPtr ; // deallocates the memory

17 Set_new_handler() function When new operator attempts to allocate memory from the heap and memory is not available, we get an out-of-memory condition Then with the help of new handler function, it throws an exception of type ‘bad_alloc’ We can specify that the new operator, upon encountering such condition, to call ‘set_new_handler()’ function and to pass the name of the desired function as parameter to it This prototype is in the ‘new.h’ header file new_handler set_new_handler (new_handler)

18 It is a data type and a function pointer type The formal argument is a function pointer If we pass the name of our desired function as a parameter to the function, all subsequent out-of- memory conditions cause the new operator to call it. Our desired function becomes the new handler When the set_new_handler() function is called, it returns a pointer to the previous new handler function An important characteristic of the new operator is that when its request for memory fails, it calls the new handler function repeatedly until its request is satisfied

19 #define BIG_NUMBER 999999 #include //specifying new handler function void myNewHandler() { //code to handle out-of-memory condition} void main() { new_handler oldHandler; //set function myNewHandler as new handler oldHandler=set_new_handler(myNewHandler); int * p =new int[BIG_NUMBER]; //probably cause } //out of memory

20 If the OS is unable to allocate the required amount of memory, the new operator fails. The new handler function gets called. Since myNewHandler is set as the new handler, it gets called We can make the function log an error message and then call the abort() which simply terminates the program void myNewHandler() { //statement to log a suitable error message abort(); // terminates the program } //defining new handler function

21 We can replace the existing new handler function by another one. For this, we can call the ‘set_new_handler()’ from with in the existing new handler. Such a call to be preceded by the code that attempts to resolve out-of memory condition #include //replacing existing new_han_fun void myNewHandler() { // make an attempt to resolve the out-of-memory condition if(above_attempt_fails) set_new_handler (myAnotherNewHandler); }


Download ppt "DYNAMIC MEMORY MANAGEMENT. int x; When the source code containing this statement is compiled and linked, an executable file is generated. When the executable."

Similar presentations


Ads by Google