Construct Sensor Packet Placement

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

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

    // Write your pool construction code here
    using Storage=aligned_storage_t<sizeof(SensorPacket),alignof(SensorPacket)>;
    Storage s[3];
    SensorPacket *p=new (&s[index]) SensorPacket{id,value};
    cout<<p->id<<" "<<p->value;
    p->~SensorPacket();
    return 0;
}

Solving Approach

 

 

 

 

 

Upvote
Downvote
Loading...

Input

1 50 900

Expected Output

50 900