Tsallaka zuwa abun ciki
Justice Tech School

Smart Pointers

unique_ptr owns one object, shared_ptr shares ownership. They free memory automatically.

Example
#include <iostream>
#include <memory>

int main() {
  auto u = std::make_unique<int>(5);
  auto s = std::make_shared<int>(7);
  auto s2 = s;
  std::cout << *u << " " << *s << " " << s.use_count() << "\n";
  return 0;
}

Result

5 7 2

Exercise

Single owner.

std::<int>

Quiz Test

What does shared_ptr count?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.