🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Memory and Smart Pointers: all 10 questions

Quiz: Memory and Smart Pointers · Question 6 of 10

What does this print? (code)

What does this print?

#include <iostream>
#include <memory>
using namespace std;
int main() {
    shared_ptr<int> a = make_shared<int>(7);
    shared_ptr<int> b = a;
    {
        shared_ptr<int> c = a;
        cout << a.use_count() << " ";
    }
    b.reset();
    cout << a.use_count() << " " << *a << "\n";
    return 0;
}
  1. A.3 3 7
  2. B.2 1 7
  3. C.3 1 7
  4. D.3 2 7
Answer & explanation

Correct answer: 3 1 7

a, b and c share the int (3). c is destroyed at the end of its block and b.reset() releases b, leaving 1 owner.

Can you get the rest right without peeking?

Take the timed Quiz: Memory and Smart Pointers quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →