Giving simple structure (POD) containing only one array of shorts (bytes, ints from <cstdint>, etc) and no more fields will be added later:
#define FIXED_SIZE 128 // 'fixed' in long term, shouldn’t change in future versions struct Foo { uint16_t bar[FIXED_SIZE]; }; is it any possibility to end up with padding at the end of the structure added by compiler for any reason ?
It seems reasonable not to make any padding as it is no any obvious need of it, but is it any guarantees by standard (could you provide any links where it is explained)?
Later I would like to use arrays of Foo structs in simple serialization (IPC) within different platforms and don't want to use any libraries for this simple task (code simplified for demonstration):
#define FOO_ELEMS 1024 ... // sender Foo *from = new Foo[FOO_ELEMS]; uint8_t *buff_to = new uint8_t[FOO_ELEMS * FIXED_SIZE * sizeof(uint16_t) ]; memcpy(buff_to, from, ...); ... // receiver uint8_t *buff_from = new uint8_t[ ... ]; Foo *to = new Foo[FOO_ELEMS]; memcpy(to, buff_from, ...); I would like to use struct here instead of plain arrays as it will be some auxiliary methods within struct and it seems more convenient then to use plain functions + arrays pointers instead.
Intersects with this (plain C) question, but seems a little bit different for me: Alignment of char array struct members in C standard
static_assert(sizeof(Foo) == FIXED_SIZE * sizeof(uint16_t), "Unexpected padding");