62. Returning unique_ptr Ownership

In many systems, a resource is created in one function and used elsewhere.
Modern C++ expresses this ownership transfer explicitly by returning a std::unique_ptr.

Your task is to correctly implement a function that:

  • Allocates a dynamic buffer
  • Initializes it using input values
  • Returns exclusive ownership to the caller using std::unique_ptr

The caller must then use the buffer without worrying about cleanup.

This problem focuses on clean ownership flow through return values.

Program Flow:

  1. Read integer N
  2. Call a function that creates and initializes a buffer of size N
  3. Receive ownership in main
  4. Print the buffer contents
  5. Exit normally (automatic cleanup)

 

Example Input:

4
10 20 30 40

Example Output:

10 20 30 40

 

Constraints:

  • N ranges from 1 to 100
  • Each buffer element value ranges from 0 to 255
  • All values fit within an unsigned 8-bit integer
  • Resource creation must happen inside the function
  • Ownership must be returned using std::unique_ptr
  • No raw pointer may escape
  • No manual delete is allowed

 

 

 

Loading...

Input

1 0

Expected Output

0