#include#include #include using namespace std; int main() { int* ptr = new int(5); cout << "Ptr value: " << *ptr << endl; unique_ptr uptr(ptr); cout << "Uptr value: " << *uptr << endl; uptr.release(); if (uptr == nullptr) { cout << "Uptr pointer released" << endl; } return 0; }
#includeIn the second example, a shared_ptr is used instead of unique_ptr, which will decrement the reference count for the managed object until it reaches zero. This is done using the reset() function. Both the unique_ptr and shared_ptr types are located in the standard library and do not require any additional packages.#include #include using namespace std; int main() { int* ptr = new int(5); cout << "Ptr value: " << *ptr << endl; shared_ptr sptr(ptr); cout << "Sptr value: " << *sptr << endl; sptr.reset(); if (sptr == nullptr) { cout << "Sptr pointer released" << endl; } return 0; }