Uint32_t 64-bit alignment?

I am interested to know about uint32_t type alignment on 64-bit platforms. The spectrum says uint32_t should be exactly the given bit width, which really seems to be:

> printf("sizeof(uint32_t): %zd\n", sizeof(uint32_t)); sizeof(uint32_t): 4 

But then I have a structure:

 typedef struct A { uint32_t a; uint32_t b; } A; 

But what is surprising:

 > printf("sizeof(A): %zd\n", sizeof(A)); sizeof(A): 16 

Is uint32_t 8-byte aligned for any reason? Is this really an 8 byte type under it?

+6
source share
2 answers

It completely depends on your compiler and architecture. In your case, it looks as if the fields are really aligned by 8 bytes, possibly for performance reasons.

+6
source

My guess is that by default everything on a 64-bit architecture will be aligned with 64-bit boundaries, just like on a 32-bit architecture, everything is aligned with 4 bytes. You can specify pragma packaging directives to get rid of filling. for instance

 #pragma pack(0) 

in gcc.

+2
source

Source: https://habr.com/ru/post/911870/


All Articles