Announcements and Reminders
|
Group Project Presentations1:30 pm Group 32:00 pm Group 1 Smart Pointers
Why?
dynamically allocated memory requires a delete - the user must take responsibility for the release of memory. automatic variables or stack memory variables - memory is automatically released when the variable goes out of scope. Solution Use automatic variables, or "wrap" your dynamically allocated memory in automatic variables. unique_ptrA unique_ptr is a class template used to hold a pointer to dynamically allocated memory.
Some unique_ptr member functions
operator* - returns a reference to the object owned by the unique_ptr operator-> - allows access to a member of the object owned by the unique_ptr get - returns the pointer to the object owned by the unique_ptr release - releases ownership of the object owned by the unique_ptr, does not release memory (possible leak) reset - releases memory for the object owned by the unique_ptr. With argument, may take on new ownership make_unique - allocates memory for an object and returns a unique_ptr to it shared_ptrA shared_ptr is a class template that allows multiple objects to point to the same dynamic memory object.
Some shared_ptr member functions
operator* - returns a reference to the object owned by the shared_ptr operator-> - allows access to a member of the object owned by the shared_ptr get - returns the pointer to the object owned by the shared_ptr use_count - returns the number of shared_ptr that share ownership of an object reset - releases ownership of a shared object. Call destructor if only one share exists make_shared - allocates memory for an object and returns a shared_ptr to it Example 17-2 - shared_ptrReading assignment: Course Notes in the Shared Pointer section, Example 3 (~Page 331) |