Podcast
Questions and Answers
What is a smart pointer?
What is a smart pointer?
An object that stores a pointer to a heap-allocated object, which deletes the pointed-to object automatically when it falls out of scope.
What does the unique_ptr
do?
What does the unique_ptr
do?
It disables its copy constructor and assignment operator to make ownership unique.
What is a shared_ptr
?
What is a shared_ptr
?
A type of smart pointer that can have multiple owners and enables copy and assignment.
What bug can occur with shared_ptrs
?
What bug can occur with shared_ptrs
?
What is a weak_ptr
?
What is a weak_ptr
?
What is an STL map container?
What is an STL map container?
What is an STL vector container?
What is an STL vector container?
Flashcards are hidden until you start studying
Study Notes
Smart Pointers
- Smart pointers manage memory and ensure automatic deallocation of pointers when they are no longer needed.
- Behave like regular pointers, supporting operator overloading for *, ->, and [].
- Automatically delete the associated heap-allocated object upon going out of scope.
unique_ptr
- A unique pointer that ensures exclusive ownership of a dynamically allocated object.
- Copy constructor and assignment operator are disabled to prevent multiple pointers from managing the same object.
- Can be transferred with std::move or reset using x.reset(y.release()) to change ownership.
shared_ptr
- Allows multiple pointers to own the same dynamically allocated object.
- Enables copying and assignment, incrementing a shared reference count with each copy.
- Reference count decreases when a shared_ptr is destroyed; if it reaches zero, the managed object is deleted.
Bugs from shared_ptr
- Issues arise when multiple shared_ptrs are created from the same raw pointer, leading to separate reference counts.
- If any shared_ptr decrements its count to zero, the managed object will be deleted, potentially leaving the other pointer as a dangling reference or causing double deletion.
weak_ptr
- Unlike shared_ptr, a weak pointer does not maintain a reference count, thus preventing circular references.
- Can only point to an object managed by a shared_ptr, and cannot be dereferenced directly.
- Commonly used in data structures like LinkedLists for "prev" pointers to simplify reference counting.
STL Map Container
- A container that stores key/value pairs, functioning like a hashmap.
- Access elements using iterators, with map.begin() and map.end() indicating the start and end of the map.
- Elements can be accessed via iterators with it.first for keys and it.second for values.
STL Vector Container
- Similar to an ArrayList, supporting dynamic resizing and element storage.
- Access elements using iterators, with vec.begin() indicating the first element and vec.end() indicating the end.
- Elements can be added using push_back() and accessed with vec.front() for the first and vec.back() for the last element.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.