190 likes | 347 Views
CMSC 202. Computer Science II for Majors. Topics. Memory management operators Dynamic memory Project 2 questions. Memory Management Operators. Control allocation and deallocation of memory for any data-type Used when we don’t know in advance how much memory space is needed
E N D
CMSC 202 Computer Science II for Majors
Topics • Memory management operators • Dynamic memory • Project 2 questions
Memory Management Operators • Control allocation and deallocation of memory for any data-type • Used when we don’t know in advance how much memory space is needed • C++ defines two unary operators – new and delete for allocating and deallocating memory
Memory Management Operators …cont • new operator • General Form: pointer-variable = newdata-type • pointer-variable is pointer of type data-type • data-type is any valid data type (including objects)
Memory Management Operators …cont • Example 1. float *floatPtr; floatPtr = new float; 2. int *arrayPtr; arrayPtr = new int[10]; 3. Time *timePtr; timePtr = new Time;
Memory Management Operators …cont • Consider pointer to array example • It creates a memory space for an array of 10 integers • arrayPtr[0] refers to first element, arrayPtr[1] refers second element… • Multidimensional arrays int **arrayPtr = new int[5][4];
Memory Management Operators …cont • Consider the Time object example • new creates an object of proper size of type Time • Calls the default constructor • Returns a pointer to Time object • Initialize a newly created object Time *timePtr = new Time (12,0,0);
Memory Management Operators …cont • delete operator • General Form: deletepointer-variable For arrays delete [ ] pointer-variable
Memory Management Operators …cont • delete destroys dynamically allocated object and frees memory space • Example delete floatPtr; delete [ ] arrayPtr; delete timePtr; • Don’t forget to use delete [ ] for arrays
Memory Management Operators …cont new *intPtr = new int (12); .. .. delete intPtr; While deallocating memory, following happens implicitly: • Value of intPtr is checked against NULL that is zero • If intPtr is not NULL, the memory segment to which intPtr points is deallocated
Memory Management Operators …cont • Memory leak for (int i=0; i < 10; i++) { int *intPtr = new int( 256 ); //... Some code } • intPtr is internal to for loop, it doesn’t exist outside the loop • Thus link to memory allocated is permanently lost – Memory Leak • Underlines the importance of matching new and delete
Memory Management Operators …cont • Segmentation Fault int *intPtr; for (int i=0; i < 10; i++) { intPtr = new int( 256 ); //... Some code delete intPtr; } //... delete intPtr; // segmentation fault Beware of such faults … core dump files
Memory Management Operators …cont • Good programming practice • Initialize pointer to NULL after declaration and after delete int *intPtr = NULL; for (int i=0; i < 10; i++) { intPtr = new int( 256 ); //... Some code delete intPtr; intPtr = NULL; } //... delete intPtr; intPtr = NULL;
Dynamic Memory • Variable representation int num; num : int n[5]; n :
Dynamic Memory struct sampleStruct { int p; char c; }; sampleStruct
Dynamic Memory … cont • Pointer int *intPtr = new int (12); intPtr : 12
Dynamic Memory … cont • Consider the code int *foo = new int* [4]; *foo[0] = 17; foo[1] = NULL; foo[2] = foo[1]; *foo[3] = 42; What would be the memory picture generated ?
Dynamic Memory … cont foo: 17 42
Dynamic Memory … cont • Memory Picture Which code generates this picture ? 1 NBA 2003 12