std::weak_ptr resolving Circular Dependencies

Hard Dynamic Memory & Smart Pointers PRO
🔒 Login to Unlock

Problem Statement

Create a `std::shared_ptr sp` based on input. Assign it to a `std::weak_ptr wp`. A weak_ptr does not increase the reference count. Call `wp.lock()` to temporarily access the shared resource. If successful, print ‘Locked: [val]’.

Input Format

A single integer.

Output Format

Print the retrieved value via lock().

Constraints

None

Sample Input

99

Sample Output

Locked: 99

Explanation

Weak pointers break circular references by observing a shared_ptr without claiming ownership. To use the data, you must 'lock' it to ensure it hasn't been destroyed.

Starter Code

#include <iostream>
#include <memory>

int main() {
    int val;
    if (std::cin >> val) {
        // Implement weak_ptr lock logic
    }
    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