Placement New Static Buffer

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

struct Block{
    int id; 
    int value;
};

// Define your struct and placement new logic here

int main() {
    alignas(Block) unsigned char buffer[sizeof(Block)];

    int id, value;
    cin >> id >> value;
    
    Block* obj=new (buffer) Block;

    obj->id=id;
    obj->value=value;

    cout<<obj->id<<" "<<obj->value;

    obj->~Block(); 
    // Write your placement new construction code here

    return 0;
}

Solving Approach

 

 

 

 

 

 

Upvote
Downvote
Loading...

Input

7 42

Expected Output

7 42