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 SensorPacket
{
    int id;
    int value;
} senor_packt;
int main() {
    int index, id, value;
    cin >> index >> id >> value;

    // Write your pool construction code here
    alignas(SensorPacket) uint8_t buffer[3*sizeof(SensorPacket)];
    senor_packt * sp = new (buffer) senor_packt{index};
    sp->id = id;
    sp->value = value;
    std::cout << id << " " << value;
    sp->~senor_packt();  
    return 0;
}

Solving Approach

 

 

 

 

 

Upvote
Downvote
Loading...

Input

1 50 900

Expected Output

50 900