First page Back Continue Last page Graphics
13.12 Class auto_ptr and Dynamic Memory Allocation
Declare pointer, allocate memory with new
- What if exception occurs before you can delete it?
- Memory leak
Template class auto_ptr
- Header <memory>
- Like regular pointers (has * and ->)
- When pointer goes out of scope, calls delete
- Prevents memory leaks
- Usage
- auto_ptr< MyClass > newPointer( new MyClass() );
- newPointer points to dynamically allocated object