63. Unique Pointer Custom Deleter

In embedded and firmware-oriented C++ systems, objects are frequently created inside factory functions and manage multiple dynamically allocated resources internally. Such objects often cannot rely on a default destructor and instead must be cleaned up using a specific cleanup routine to ensure deterministic and correct resource release.

You are given:

  • A class that dynamically allocates two internal buffers
  • A factory function that creates the object dynamically
  • A custom cleanup function that must be used to destroy the object correctly

Your task is to use std::unique_ptr with a custom deleter so that:

  • The object is created inside a function
  • Ownership is transferred safely to the caller
  • Cleanup happens automatically when the object goes out of scope
  • The custom cleanup function is invoked exactly once
  • No manual calls to delete or cleanup() are made

Program Flow:

  1. Read integer N
  2. Call a factory function that dynamically allocates and returns an object
  3. Store the returned object in a std::unique_ptr with a custom deleter
  4. Read N integer values and store them in the object
  5. Print the stored values
  6. Exit the scope
  7. Custom cleanup must execute automatically

Input:

  • Integer N (1 ≤ N ≤ 100)
  • N space-separated integers on the next line

Notes:

  • Values are stored in uint8_t buffers
    (values outside the range 0–255 are truncated modulo 256)
  • Object allocation must occur inside the factory function
  • Do not manually call delete or cleanup()

Output:

  • One line containing the stored values separated by spaces
  • A newline
  • The exact text:

    Object cleaned
    

 

Example Input:

3
10 20 30 

Example Output:

10 20 30
Object cleaned

 

Constraints:

  • 1 ≤ N ≤ 100
  • The object owns multiple dynamically allocated members
  • Cleanup must occur through the provided cleanup function
  • Output format must match exactly

 

 

 

 

Loading...

Input

1 0

Expected Output

0 Object cleaned