- Dynamic memory management
- Control allocation and deallocation of memory
- Operators new and delete
- Include standard header new; Access to standard version of new
- new
- Consider
- Time *timePtr;
- timePtr = new Time;
- new operator
- Creates object of proper size for type Time; Error if no space in memory for object
- Calls default constructor for object
- Returns pointer of specified type
- Providing initializers
- double *ptr = new double( 3.14159 );
- Time *timePtr = new Time( 12, 0, 0 );
- Allocating arrays; int *gradesArray = new int[ 10 ];
- delete
- Destroy dynamically allocated object and free space
- Consider; delete timePtr;
- Operator delete
- Calls destructor for object
- Deallocates memory associated with object; Memory can be reused to allocate other objects
- Deallocating arrays
- delete [] gradesArray; ; Deallocates array to which gradesArray points
- If pointer to array of objects
- First calls destructor for each object in array
- Then deallocates memory
2004-07-08