Recitation - Mar 14, 2011
- mechanism to adjust the amount of memory allocated to a program
- need to maintain at least one reference (e.g. pointer) to dynamically allocated memory
- lack of any reference to dynamically allocated memory is known as a memory leak
- dynamically allocated member is typically located outside the object
- not automatically "cleaned up"
- generally a good idea to initialize pointers in the constructor.
- destructor commonly deallocates dynamically created memory.
- destructor is the last function that runs for an object
- Common steps for a dynamically allocated and resizeable array:
- new - create new array of desired size
- copy - copy old contents into new array
- delete - deallocate old array
- adjust pointers - assign array pointer to newly allocated array
Copying
- shallow vs deep copies, what is the difference?
- copy constructor
- copy assignment operator
- example
More examples
Additional Notes
- Assignement 5
- Do not wait till the last minute!
- Design your solution before coding.
- Separate out memory management from the algorithm implementation.
- Restrict new/delete to a select set of functions.
- Make sure every new has a corresponding delete.