dynamic memory management. int x; when the source code containing this statement is compiled and...

21
DYNAMIC MEMORY MANAGEMENT

Upload: elinor-tate

Post on 16-Dec-2015

221 views

Category:

Documents


1 download

TRANSCRIPT

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

DYNAMIC MEMORY MANAGEMENT

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

• 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.

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

• 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 itselfDynamic memory allocation

• It is achieved in C through malloc(), calloc() and realloc() functions

• In C++ it is achieved through ‘new’ operator

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

#include <iostream.h>void main(){

int * iPtr;iPtr= new int ;*iPtr = 10;cout << *iPtr << endl;

} // end of dynamic.cpp• Output :

10

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

• ‘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 <pointer> = new <data_type>

• The new operator can be used to create multiple blocks of memory also<pointer> = new <data_type>[<num_of_ele>]

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

#include <iostream.h># define SIZE 10void 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

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

#include <iostream.h> //size specified at runtimevoid 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

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

#include <iostream.h> //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 ;

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

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.

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

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 <pointer>• 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

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

#include <iostream.h> void abc(); void main(){ abc();

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

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

• 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

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

• 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 referencevoid abc ( int ** p) { // some complex algorithm*p = new int ; // rest of abc() function }

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

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

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

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

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

• 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[] <pointer> ;

Distance * dPtr ;dPtr = new Distance[5] ;//creates an arraydelete [] dPtr ; // deallocates the memory

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

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 filenew_handler set_new_handler (new_handler)

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

• 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

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

#define BIG_NUMBER 999999 #include<new.h> //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

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

• 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

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

• 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<new.h> //replacing existing new_han_funvoid myNewHandler(){ // make an attempt to resolve the out-of-memory

condition if(above_attempt_fails)

set_new_handler (myAnotherNewHandler); }