#include<stdio.h>
struct A
{
char c;
double e;
int s;
}A;
int main()
{
printf("%u\n", sizeof(A));
return 0;
}
It is giving output 16. Shouldn't it be 24 if we consider structure internal padding and structure padding as a whole?
I am compiling the code on Ubuntu 14.04 32 bit with GCC 4.8.2.
Your calculations assume that double
needs to be 8-byte aligned. That's not the case on all architectures.
On 32bit x86 Linux with GCC, double
will be 4-byte aligned by default. You can change that with the -malign-double
flag to make it 8-byte aligned.
So the layout assuming defaults on 32bit x86 Linux:
char // 1 byte
// 3 byte padding
double // 8 bytes
int // 4 bytes
So a total of 16 bytes, with 3 bytes of padding in the middle.
The Wikipedia article Data structure alignment has size/alignment numbers for various types on 32bit x86 and 64bit x86_64 in a few compilers/environments.
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