diff options
Diffstat (limited to 'graphs/cpp/smtptr/main.cc')
| -rw-r--r-- | graphs/cpp/smtptr/main.cc | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/graphs/cpp/smtptr/main.cc b/graphs/cpp/smtptr/main.cc new file mode 100644 index 0000000..a574d45 --- /dev/null +++ b/graphs/cpp/smtptr/main.cc @@ -0,0 +1,20 @@ +#include "shared_pointer.hh" + +int main() +{ + // Lifetime of the data + { + SharedPointer<int> p2; + { + SharedPointer<int> p1{ new int{ 5 } }; + p2 = p1; // Now both SharedPointers own the memory + } + // p1 is freed, but the int pointer remains + // Memory still exists, due to p2 + std::cout << "Value of p2: " << *p2 << std::endl; // Output: 5 + } + // p2 is freed, and since no one else owns the memory, + // the int pointer is freed too + + return 0; +} |
