#1816easyC++ & Systems
Reordering Members to Shrink a Struct
A market-data feed handler stores every tick in the struct below. The record is written and read millions of times per second, so its size directly determines cache footprint and memory bandwidth.
#include <cstdint>
struct Tick {
char side; // 'B' for buy, 'S' for sell
double price;
std::int32_t qty;
char flags;
};
struct TickReordered {
double price;
std::int32_t qty;
char side;
char flags;
};
Assume a typical 64-bit platform (for example x86-64 Linux with gcc or clang): sizeof(char) == 1, sizeof(std::int32_t) == 4 with alignof(std::int32_t) == 4, and sizeof(double) == 8 with alignof(double) == 8. No packing pragmas or attributes are in effect.
What are sizeof(Tick) and sizeof(TickReordered)?
Loading interactive editor…