the orders of members in a struct affect sizeof of struct
see this example https://www.joshcaratelli.com/blog/struct-packing
if no compiler directive - memory align to 8 bytes by default
struct top
{
char* p; //8 bytes
int i; //4 bytes
short s; // 2 bytes
char c; // 1 byte
// 1 padding
int t; // 4 bytes
// 4 padding
};
struct buttom
{
char c; // 1 bytes -- 7 padding
char* p; //8 bytes
short s; //2 bytes
int i; //4 bytes
// 2 padding
int t; // 4 bytes
// 4 padding
};
int sizeTop = sizeof(top); // =>24
int sizeButtom = sizeof(buttom); // =>32
No comments:
Post a Comment