59. unique_ptr RAII Basics

You are given a program that allocates a dynamic buffer inside a limited scope.

Your task is to manage this buffer using std::unique_ptr so that the memory is released automatically when the scope ends, without calling delete.

This problem introduces the core idea of RAII (Resource Acquisition Is Initialization), where resource lifetime is tied to object lifetime.

Program Flow:

  1. Read integer N
  2. Enter a local scope
  3. Allocate a dynamic buffer of N integers
  4. Read N integers into the buffer
  5. Print the buffer contents
  6. Exit the scope
  7. Print confirmation after the scope ends

 

Example Input:

4
10 20 30 40

Example Output:

10 20 30 40 
Scope ended

 

Constraints:

  • N ranges from 1 to 100
  • Input values fit within 32-bit signed integers
  • Use std::unique_ptr for ownership
  • Do not use delete explicitly
  • Output format must match exactly

 

 

 

Loading...

Input

1 0

Expected Output

0 Scope ended