26. Reinitialize Object In-Place

In embedded firmware, some objects (such as state machine nodes or mode configurations) must be reinitialized without changing their memory location.

Heap allocation is not allowed. Instead, placement new is used to construct objects directly inside a preallocated static buffer.

You must:

  1. Define a struct Mode with a single integer field level.
  2. Create a statically allocated, properly aligned buffer large enough to hold one Mode object.
  3. Use placement new to construct the first Mode object in the buffer, initializing level with the first input.
  4. Manually call the destructor of the object.
  5. Reconstruct a second Mode object in the same buffer, initializing level with the second input.
  6. Manually call the destructor of the object.
  7. Print the final mode level.

 

Example 

Input:

3 9

Output:

9

 

Constraints:

  • Placement new must be used for both constructions.
  • The destructor must be called manually before reconstruction.
  • The buffer must be correctly aligned for Mode.
  • No heap allocation is allowed.

 

 

 

 

Loading...

Input

3 9

Expected Output

9