Placement New Static Buffer

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

// Define your struct and placement new logic here
struct Block{
    int id;
    int value;

    //Constructor
    Block(int i, int v) : id(i), value(v) {}
    ~Block() {}
};

alignas(Block) static uint8_t buffer[sizeof(Block)];

int main() {
    int id, value;
    cin >> id >> value;

    Block* b = new Block(id, value);
    cout << b->id << " " << b->value;
    b->~Block();

    // Write your placement new construction code here

    return 0;
}

Solving Approach

 

 

 

 

 

 

Upvote
Downvote
Loading...

Input

7 42

Expected Output

7 42