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() {
    int id, value;
    cin >> id >> value;

   alignas(Block) char buffer[sizeof(Block)];

    // Write your placement new construction code here
    Block* b = new (buffer) Block();

    b->id = id;
    b->value = value;

    std:cout << b->id <<" "<< b->value ;

    b->~Block();

    return 0;
}

Solving Approach

 

 

 

 

 

 

Upvote
Downvote
Loading...

Input

7 42

Expected Output

7 42