If you are like me, you know raw pointers look like this: `int * someNumber`. You manage your own memory with `new` and `delete`. Manual memory management has its' benefits, but clearly it does not at a large scale given the amount of overhead people are willing to take to use garbage collected languages.
If you know about auto_ptr, forget about it. It is gone in C++17.
Unique, Shared and Weak Pointers are the new memory management for C++, so semi-automatic garbage collection! Basically ARC from Objective-C though Boost may have been the inspiration for both. All of these pointers require the <memory> header. There is a link below for the C++ memory reference.
Unique pointer works in that you assign an owner (scope) to the pointer. When we exit from that scope, then the pointer and its' contents are freed.
How do I use it? There are two ways to instantiate this guy:
- std::unique_ptr uptr(new Object());
How do I use it? Same way as unique.
- std::shared_ptr sptr(new Object());
How do I use it?
- std::weak_ptr<Object> wptr = sptr;
This is your friendly Intro to Modern C++ pointers. Stop using raw pointers. :) Use the link below as your bible.
- CPP Memory Ref: http://en.cppreference.com/w/cpp/memory