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;
};

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

    // Write your placement new construction code here
    char buffer[sizeof(Block)];
    Block * blockPtr = new(buffer) Block; //constructs the object in-place

    blockPtr->id = id;
    blockPtr->value = value;

    cout << id << " " << value << endl;

    blockPtr->~Block();

    blockPtr = nullptr;

    return 0;
}

Solving Approach

 

 

 

 

 

 

Upvote
Downvote
Loading...

Input

7 42

Expected Output

7 42