Placement New Static Buffer

#include <iostream>
#include <cstdint>
#include <new>
using namespace std;

// Define your struct and placement new logic here
typedef struct Block{
    int id, value;
};
int main() {
    int id, value;
    cin >> id >> value;

    // Write your placement new construction code here
    alignas(Block) uint8_t buffer[sizeof(Block)];
    Block* ptr = new (buffer) Block;
    ptr->id = id;
    ptr->value = value;
    
    // Output fields
    cout << ptr->id << " " << ptr->value;

    // Explicit destructor call (required for placement new)
    ptr->~Block();
    return 0;
}

Solving Approach

 

 

 

 

 

 

Upvote
Downvote
Loading...

Input

7 42

Expected Output

7 42