How can I know the actual structure size? using sizeof returns the number of bytes after alignment. For example :
struct s {
char c;
int i
}
sizeof(s) = 8; I am interested to get the size of bytes without alignment ,i.e 5
The actual size of structure is size of structure after alignment. I mean sizeof
return size of structure in the memory. if you want to have an structure that aligned on byte you should tell the compiler to do that. for example something like this:
#ifdef _MSVC
# pragma pack(push)
# pragma pack(1)
# define PACKED
#else
# define PACKED __attribute((packed))
#endif
struct byte_aligned {
char c;
int i;
} PACKED;
#ifdef _MSVC
# pragma pack(pop)
#endif
now sizeof(byte_aligned)
return 5 and its actually 5 byte in memory
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With