Construct Sensor Packet Placement

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

// Write your struct and placement new logic here
struct SensorPacket {
    int id;
    int value;
};
int main() {
    int index, id, value;
    cin >> index >> id >> value;
    alignas(SensorPacket) uint8_t buff[sizeof(SensorPacket)*3];
    // Write your pool construction code here
    uint8_t* location = buff + (index * sizeof(SensorPacket));
    SensorPacket* ptr = new (location) SensorPacket{id, value};
    cout << ptr->id << " " << ptr->value;
    ptr->~SensorPacket();
    return 0;
}

Solving Approach

 

 

 

 

 

Upvote
Downvote
Loading...

Input

1 50 900

Expected Output

50 900