83. Move Constructor Buffer Transfer

In embedded firmware systems, sensor data buffers are frequently passed between tasks, queues, or ISR-to-thread boundaries. These buffers often own dynamically allocated memory, and copying them can be expensive on memory- and performance-constrained hardware.

To reduce overhead, such systems rely on move semantics, allowing ownership of resources to be transferred instead of copied.

You are given a SensorBuffer class that owns a dynamically allocated byte buffer. Your task is to implement a move constructor that transfers ownership of the buffer safely and efficiently.

After implementing the move constructor:

  • Ownership of the buffer must be transferred to the destination object
  • No deep copy of the buffer must occur
  • The source object must be left in a valid, empty state
  • No memory leak must occur
  • No double-free must occur when objects are destroyed

Program Flow:

  1. Read integer N
  2. Read N bytes
  3. Construct buffer A with size N
  4. Move-construct buffer B from A
  5. Print buffer A
  6. Print buffer B

Expected Behavior After Move:

  • Buffer A prints: No data
  • Buffer B prints the original byte sequence

Example Input:

5
10 20 30 40 50 

Example Output:

No data
10 20 30 40 50 

Constraints:

  • 1 ≤ N ≤ 100
  • Buffer memory must be allocated using new[]
  • The move constructor must transfer ownership only (no copying)
  • The source object must be safe to destroy after being moved from
  • If the buffer is empty, output must be exactly:

    No data 
  • Output formatting must match exactly, including spaces and newlines

 

 

 

 

Loading...

Input

1 42

Expected Output

No data 42