Placement New Static Buffer

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

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

    alignas(Block) uint8_t buf[sizeof(Block)];
    Block* p=new (buf) Block{id,value};
    cout<<p->id<<" "<<p->value;
    p->~Block();
    return 0;
}

Solving Approach

 

 

 

 

 

 

Upvote
Downvote
Loading...

Input

7 42

Expected Output

7 42