Transferring Ownership with std::move

Medium Dynamic Memory & Smart Pointers
Solve in Playground →

Problem Statement

Create a `std::unique_ptr ptr1` containing an integer read from input. Use `std::move` to transfer ownership of the pointer to `ptr2`. Check if `ptr1` is now null; if so, print ‘ptr1 empty’. Then print ‘ptr2: [val]’.

Input Format

A single integer.

Output Format

Print the status of both pointers.

Constraints

None

Sample Input

77

Sample Output

ptr1 empty
ptr2: 77

Explanation

Because unique_ptrs are exclusive, they cannot be copied. Their ownership must be explicitly 'moved' to another pointer, leaving the original pointer empty.

Starter Code

#include <iostream>
#include <memory>
#include <utility>

int main() {
    int val;
    if (std::cin >> val) {
        // Transfer ownership using std::move and verify
    }
    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