0

I want to know, is it acceptable to organize data in following manner? Structures is not POD types anymore, due to member initializers, and compiler gives warning about packed attribute being ignored. Is it possible to make them guaranteed packed?

struct StructOne
{
    const quint8 h{0x11};
} __attribute__((packed));

struct StructTwo
{
    const quint8 h{0x22};
} __attribute__((packed));

struct StructThree
{
    quint8 s{0x33}
    union
    {
        StructOne s1;
        StructTwo s2;
    } __attribute__((packed));
} __attribute__((packed));

struct Top
{
    union
    {
        StructThree s3;
        quint8 data[2];
    } __attribute__((packed));
} __attribute__((packed));
  • Currently the size of `Top` is 2 bytes assuming `qunit8` to be the size of a `char`. You expect it to go lower than that? – P.W Nov 23 '18 at 06:28
  • @P.W I expect that it wouldn't be greater, that 2 bytes. Is this structure still be packed? – Egor.Uttsov Nov 23 '18 at 12:16
  • It can't go lower than 2 bytes. The size of the union will be at least 2 bytes. So I believe its packed already. – P.W Nov 23 '18 at 12:21
  • @P.WThe main question - is it possible to receive a padding on some platforms? – Egor.Uttsov Nov 23 '18 at 12:56
  • I think it depends on whether the compiler can guarantee "packedness" with this attribute, then platform will not matter. – P.W Nov 23 '18 at 12:59
  • @P.W OK. Thanks. I can conclude, that this code is not portable and not guaranteed to be packed. – Egor.Uttsov Nov 23 '18 at 13:14
  • While writing portable code, it is better to assume the worst. But I don't what sort of guarantee GCC provides. – P.W Nov 23 '18 at 13:16

0 Answers0