std::shared_ptr Basics

Medium Dynamic Memory & Smart Pointers
Solve in Playground →

Problem Statement

Create a `std::shared_ptr sp1` holding a value read from input. Print its reference count using `use_count()`. Then create a second `std::shared_ptr sp2 = sp1;` and print the `use_count()` of `sp2`.

Input Format

A single integer.

Output Format

Print 'Count1: [val]' then 'Count2: [val]'.

Constraints

None

Sample Input

10

Sample Output

Count1: 1
Count2: 2

Explanation

A shared pointer allows multiple pointers to own the same resource. It tracks how many pointers are looking at it, only deleting the memory when the count reaches 0.

Starter Code

#include <iostream>
#include <memory>

int main() {
    int val;
    if (std::cin >> val) {
        // Implement shared_ptr logic and use_count check
    }
    return 0;
}

Limits

  • Time Limit: 1s
  • Memory Limit: 256MB

Embedded C Programming

Updated: March 15, 2026
Intermediate

Embedded systems rely on efficient low-level programming to interact directly with hardware. In this course, you will learn how to write practical Embedded C programs used in real microcontroller-based systems. Rather than focusing only on theory, this course follows a practice-driven approach. Each lesson includes hands-on coding exercises that simulate real firmware development tasks used