#include <iostream>
#include <cstdint>
#include <new>
#include <type_traits>
using namespace std;
// Define the SensorPacket struct
struct SensorPacket {
int id;
int value;
// Constructor
SensorPacket(int i, int v) : id(i), value(v) {}
// Destructor
~SensorPacket() {}
};
// Create a properly aligned memory pool for 3 SensorPacket objects
alignas(SensorPacket) unsigned char pool[3 * sizeof(SensorPacket)];
int main() {
int index, id, value;
cin >> index >> id >> value;
// Ensure index is valid (0–2)
if (index < 0 || index > 2) {
cerr << "Invalid index" << endl;
return 1;
}
// Calculate the address of the chosen slot
void* slot = pool + index * sizeof(SensorPacket);
// Construct SensorPacket using placement new
SensorPacket* packet = new (slot) SensorPacket(id, value);
// Print the packet’s id and value
cout << packet->id << " " << packet->value << endl;
// Manually call the destructor
packet->~SensorPacket();
return 0;
}
Input
1 50 900
Expected Output
50 900