Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Memory management operators Dynamic memory Project 2 questions.

Similar presentations


Presentation on theme: "CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Memory management operators Dynamic memory Project 2 questions."— Presentation transcript:

1 CMSC 202 Computer Science II for Majors

2 CMSC 202UMBC Topics Memory management operators Dynamic memory Project 2 questions

3 CMSC 202UMBC 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

4 CMSC 202UMBC Memory Management Operators …cont new operator General Form: pointer-variable = new data-type pointer-variable is pointer of type data-type data-type is any valid data type (including objects)

5 CMSC 202UMBC Memory Management Operators …cont Example 1. float *floatPtr; floatPtr = new float; 2. int *arrayPtr; arrayPtr = new int[10]; 3. Time *timePtr; timePtr = new Time;

6 CMSC 202UMBC 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];

7 CMSC 202UMBC 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);

8 CMSC 202UMBC Memory Management Operators …cont delete operator General Form: delete pointer-variable For arrays delete [ ] pointer-variable

9 CMSC 202UMBC 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

10 CMSC 202UMBC 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

11 CMSC 202UMBC 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

12 CMSC 202UMBC 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

13 CMSC 202UMBC 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;

14 CMSC 202UMBC Dynamic Memory Variable representation int num; num : int n[5]; n :

15 CMSC 202UMBC Dynamic Memory struct sampleStruct { int p; char c; }; sampleStruct

16 CMSC 202UMBC Dynamic Memory … cont Pointer int *intPtr = new int (12); intPtr : 12

17 CMSC 202UMBC 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 ?

18 CMSC 202UMBC Dynamic Memory … cont foo: 17 42

19 CMSC 202UMBC Dynamic Memory … cont 12 Memory Picture Which code generates this picture ? NBA 1 2003


Download ppt "CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Memory management operators Dynamic memory Project 2 questions."

Similar presentations


Ads by Google