0

I have a structure

struct abc{
int32_t status;
union A;
};

sizeof(int32_t) is 4. sizeof(union A) is 24.

I was expecting that status should get aligned and take total 8 bytes (considering 64 bit compiler) but the size of structure abc is coming as 28 and not 32. Can someone explain why ? Note it is not pragma packed.

union A{
int8_t a;
struct b;
struct c;
}

struct b
{
int8_t a;
int32_t b2;
int32_t b3;
int32_t b4;
}

struct c
{
struct b;
int32_t c1;
int32_t c2;
}
Cool Camel
  • 57
  • 6
  • What's the definition of `structure`? – dbush Oct 06 '22 at 12:05
  • @dbush updated the question. Please check – Cool Camel Oct 06 '22 at 12:12
  • 3
    A 64 bit compiler does not necessarily imply 64 bit alignment. – nielsen Oct 06 '22 at 12:13
  • so can it be 4 bytes aligned too ? How do I check that ? – Cool Camel Oct 06 '22 at 12:14
  • When you say "`sizeof(structure A) is 24`", are you talking about *`union A`*? What is this `status` that you suppose should consume 8 bytes? The member of `struct abc`? Why would an `int32_t` occupy other than exactly 32 bits? The size of what is surprising you by being 28 bytes instead of 32? – John Bollinger Oct 06 '22 at 12:21
  • @JohnBollinger I thought that the alignment is compiler dependent and it will be 8 but as the answer explained it can even be lesser than that. – Cool Camel Oct 06 '22 at 12:23
  • @nielsen can you point me to the reference ? – Cool Camel Oct 06 '22 at 12:24
  • Alignment *is* compiler-dependent. And datatype dependent. But that does not change the fact that some of the specifics of your question (still) don't make sense. – John Bollinger Oct 06 '22 at 12:24
  • @JohnBollinger I edited the question, does that make sense now? – Cool Camel Oct 06 '22 at 12:26
  • It's still a bit muddled, but I can work out what you're trying to ask. – John Bollinger Oct 06 '22 at 12:30
  • @JohnBollinger can you point me to a good reference which tells more about "Alignment is compiler-dependent. And datatype dependent" – Cool Camel Oct 06 '22 at 12:35
  • @CoolCamel, the definitive reference is section 6.2.8 of the C language specification. The official spec itself is not available for free, but [this late draft](https://web.archive.org/web/20181230041359if_/http://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf) is almost identical. Be warned, however: that document is very technical. On the other hand, I can summarize in simpler terms: alignment is compiler dependent and datatype dependent. – John Bollinger Oct 06 '22 at 14:15
  • On modern compilers you can use `_Alignof` to learn the alignment requirements of any type on that system. Works just like `sizeof`. When applying it to your structs/union on gcc x86_64 I get alignment of 4. – Lundin Oct 06 '22 at 14:31
  • @Lundin that helped thanks. John thanks to you too! – Cool Camel Oct 06 '22 at 14:58

1 Answers1

0

The largest field in any substructure is 4 bytes in size, so the structures will have 4 byte alignment.

Going from the bottom up, the largest field in struct b is a int32_t, so struct b has 4 byte alignment.

struct c contains int32_t which is 4 bytes and struct b which has 4 byte alignment, so struct c has 4 byte alignment.

union A has a uint8_t which has 1 byte alignment, struct b which has 4 byte alignment, and struct c which has 4 byte alignment, so union A has 4 byte alignment.

Finally, struct abc has a int32_t which is 4 bytes and union A which has 4 byte alignment, so struct abc has 4 byte alignment.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • thanks for your answer. I have one more doubt if one of my substructures contain a pointer int8_t* and it's a 64 bit compiler what will be the alignment of the structure ? – Cool Camel Oct 06 '22 at 12:18
  • @CoolCamel If `sizeof(int8_t *)` is 8 then any structure containing it will have at least 8 byte alignment. – dbush Oct 06 '22 at 12:20
  • can you point me to a reference that says alignment is dependent on the alignment of elements in substructure ? Would be really helpful to see some examples etc. – Cool Camel Oct 06 '22 at 12:21
  • @CoolCamel There's no hard rule on it, just a general way of operating. See [the lost art of structure packing](http://www.catb.org/esr/structure-packing/) for details. – dbush Oct 06 '22 at 12:23
  • @CoolCamel See also this question and answer where dbush is more precise regarding the C standard: https://stackoverflow.com/questions/47662791/are-there-any-guarantees-about-c-struct-packing – nielsen Oct 06 '22 at 13:27