LearnFrenzy
Sign in Take a free test

Smart Pointers · Predict the Output · Question 1

use_count through nested scopes Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 1 Back to Exercise

What is the output of the following C++ program on a 64-bit system?

main.cpp
#include <iostream>
#include <memory>

int main() {
    auto a = std::make_shared<int>(5);
    std::cout << a.use_count();
    {
        auto b = a;
        auto c = b;
        std::cout << a.use_count();
        c.reset();
        std::cout << a.use_count();
    }
    std::cout << a.use_count() << '\n';
    return 0;
}
  • A 1321 Correct
  • B 1331
  • C 1221
  • D 1320
Correct Answer: Option A

Copies add owners; reset() removes one; leaving the scope destroys b.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links