1 / 19

CMSC 202

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

theta
Download Presentation

CMSC 202

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CMSC 202 Computer Science II for Majors

  2. Topics • Memory management operators • Dynamic memory • Project 2 questions

  3. 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. 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)

  5. 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. 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. 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. Memory Management Operators …cont • delete operator • General Form: deletepointer-variable For arrays delete [ ] pointer-variable

  9. 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. 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. 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. 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. 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. Dynamic Memory • Variable representation int num; num : int n[5]; n :

  15. Dynamic Memory struct sampleStruct { int p; char c; }; sampleStruct

  16. Dynamic Memory … cont • Pointer int *intPtr = new int (12); intPtr : 12

  17. 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. Dynamic Memory … cont foo: 17 42

  19. Dynamic Memory … cont • Memory Picture Which code generates this picture ? 1 NBA 2003 12

More Related