84. Move Assignment Ownership

You are given a C++ class that manages a dynamically allocated array of bytes.
This buffer represents an exclusively owned resource, meaning:

  • At any time, only one object may own the buffer
  • The buffer must never be duplicated
  • Copying ownership is strictly forbidden
  • Ownership transfer must be done using move semantics only

Two objects of this class already exist. Your task is to ensure that ownership of the buffer can be safely transferred from one object to another using move assignment.

 

Your Objectives

You must modify the provided template code to:

  • Implement a move assignment operator
  • Explicitly disable copy assignment
  • Ensure the destination object releases any previously owned memory
  • Transfer ownership without copying data
  • Leave the moved-from object in a valid, empty state
  • Prevent memory leaks, double frees, or undefined behavior

⚠️ The provided template code intentionally violates exclusive ownership rules and must fail the test cases.
Only a correct implementation will pass.

 

Program Flow

  1. Read integer N
  2. Read N integers and construct object A
  3. Read integer M
  4. Read M integers and construct object B
  5. Execute move assignment: A = std::move(B)
  6. Print contents of A
  7. Print contents of B

 

Expected Behavior After Fix

  • A contains the values originally stored in B
  • B prints No data
  • No deep copy occurs
  • No memory leak or double free occurs
  • Ownership is transferred, not duplicated

 

Example Input

4
10 20 30 40 3 1 2 3 

Example Output

1 2 3
No data

 

Constraints

  • N and M are in the range 1 to 100
  • Memory must be allocated using new[]
  • Memory must be released using delete[]
  • Copy assignment must be explicitly disabled
  • Move assignment must be implemented
  • Output must match exactly (including spacing and newlines)

 

 

 

Loading...

Input

4 10 20 30 40 3 1 2 3

Expected Output

1 2 3 No data