Bifalgorithm post
Okay, smart pointers as fast as possible. So smart pointers are pretty similar to raw pointers that you may have seen before, but they have some key differences. Firstly, and this is usually the best reason for using them, is that they automatically manage memory, which means there's no need to release memory manually. They also have different ownership states, where you can have unique ownership, which means only one smart pointer can have access to a resource at a time, or a shared pointer where multiple pointers can have access to a resource at a time, whereas rule pointers have no inherent ownership whatsoever. Smart pointers end up being generally preferred because of their safety and convenience. So our code here just has a rule pointer that points to an integer of five and we can output the value of the rule pointer, which gives us five. When you're done using a rule pointer, you need to clean it out of memory using the delete keyword. So this deletes the roll pointer. And this is the same code using a unique pointer, which is a variant of a smart pointer. And the most important part is that when I'm finished using my smart pointer, I don't need to delete any memory whatsoever.
Okay, smart pointers as fast as possible. So smart pointers are pretty similar to raw pointers that you may have seen before, but they have some key difference…
Ideas in this flowchart
- Smart Pointers Overview
- Comparison with Raw Pointers
- Memory Management
- Ownership States
- Unique Ownership
- Shared Ownership
- Raw Pointers
- Safety and Convenience
- Example Code
- Raw Pointer Example
- Unique Pointer Example