Size of a structure is varied ...??
Start with an example :
struct OS32 {
bool a;
char *b;
int c;
};
Size calculation on 32 bit OS :
sizeof(a) = 1 Byte
sizeof(b) = 4 Bytes
sizeof(c) = 4 bytes.
But
sizeof(OS32) = 12 Bytes
means sizeof(OS32) != sizeof(bool) + sizeof(char *) + sizeof(int).
Reason: Reason is Data Alignment : for more detail read http://en.wikipedia.org/
What is done here is data padding by the compiler to make OS32 4 byte align
i.e.
bool a // 1 Byte + 3 Bytes (added by compiler for data alignment)
char *b // 4 bytes
int c // 4 Bytes
which is now equal to 12 Bytes.
Some more example you can try :
struct OS32 {
bool a;
char *b;
int c;
bool d;
};
then here sizeof(OS32) = 16 Bytes (ya 3 + 3 byte padding)
But
struct OS32 {
bool a;
bool x;
bool y;
bool z;
char *b;
int c
};
then sizeof(OS32) = 12 Bytes (Bcz no padding is required in continuous read).
Note : Data padding is based on size of the pointer. i.e in 32 bit OS, size of pointer is 4 bytes so the data is 4 byte aligned by the compiler. In case of 64 bit OS, size of pointer is 8 Bytes so data is 8 byte aligned by the compiler.
i.e sizeof (OS32) in 64 OS = 24 bits.
Explanation :
bool a; // 1 + 7 bytes (Data padding)
char *b // 8 Byes
int c // 4 bytes + 4 bytes (Data padding)
Hence size of OS32 is 24 on 64 bit OS.
Also if we rearrange the field in stuct i.e
struct OS64 {
char *b; // 8 Bytes
int c; // 4 Bytes
bool a; // 1 Bytes + 3 Bytes padding
};
then sizeof(OS64) = 16 bytes
Hope it will help you in understanding that size of a struct is not actually equal to its size of members.
Thanx
struct OS32 {
bool a;
char *b;
int c;
};
Size calculation on 32 bit OS :
sizeof(a) = 1 Byte
sizeof(b) = 4 Bytes
sizeof(c) = 4 bytes.
But
sizeof(OS32) = 12 Bytes
means sizeof(OS32) != sizeof(bool) + sizeof(char *) + sizeof(int).
Reason: Reason is Data Alignment : for more detail read http://en.wikipedia.org/
What is done here is data padding by the compiler to make OS32 4 byte align
i.e.
bool a // 1 Byte + 3 Bytes (added by compiler for data alignment)
char *b // 4 bytes
int c // 4 Bytes
which is now equal to 12 Bytes.
Some more example you can try :
struct OS32 {
bool a;
char *b;
int c;
bool d;
};
then here sizeof(OS32) = 16 Bytes (ya 3 + 3 byte padding)
But
struct OS32 {
bool a;
bool x;
bool y;
bool z;
char *b;
int c
};
then sizeof(OS32) = 12 Bytes (Bcz no padding is required in continuous read).
Note : Data padding is based on size of the pointer. i.e in 32 bit OS, size of pointer is 4 bytes so the data is 4 byte aligned by the compiler. In case of 64 bit OS, size of pointer is 8 Bytes so data is 8 byte aligned by the compiler.
i.e sizeof (OS32) in 64 OS = 24 bits.
Explanation :
bool a; // 1 + 7 bytes (Data padding)
char *b // 8 Byes
int c // 4 bytes + 4 bytes (Data padding)
Hence size of OS32 is 24 on 64 bit OS.
Also if we rearrange the field in stuct i.e
struct OS64 {
char *b; // 8 Bytes
int c; // 4 Bytes
bool a; // 1 Bytes + 3 Bytes padding
};
then sizeof(OS64) = 16 bytes
Hope it will help you in understanding that size of a struct is not actually equal to its size of members.
Thanx
Comments
Post a Comment