1
 typedef struct structA 
{
   char        C;
   double      D;
   int         I;
} structA_t;
 

Size of this structA_t structure:

sizeof(char) + 7 byte padding + sizeof(double) + sizeof(int) = 1 + 7 + 8 + 4 = 20 bytes

But this is wrong , the correct is

24

. Why?

Community
  • 1
  • 1
gpuguy
  • 4,607
  • 17
  • 67
  • 125
  • Alignment to 8 byte is possible. –  Nov 27 '13 at 09:50
  • 5
    extra `4` for padding after `int` – Raju Kunde Nov 27 '13 at 09:52
  • @RajuKundhe why is that 4 required after int? can you elaborate and put as an answer? – gpuguy Nov 27 '13 at 09:56
  • 1
    Please have a look on [Structure Padding](http://stackoverflow.com/questions/6968468/padding-in-structures-in-c) and [padding](http://en.wikipedia.org/wiki/Data_structure_alignment#Data_structure_padding). – Dayal rai Nov 27 '13 at 10:01
  • @gpuguy the word size is 8 Bytes (processor dependent). int is 4 Bytes long (platform dependent), so it must be padded by 4 extra Bytes, the same way `sizeof(char)=1 Byte` must be padded by 7 Bytes – H_squared Nov 27 '13 at 10:19

4 Answers4

2

There is most likely 4 byte padding after the last ìnt.

If sizeof(double) == 8 then likely alignof(double) == 8 also on your platform. Consider this situation:

structA_t array[2];

If size would be only 20, then array[1].D would be misaligned (address would be divisible by 4, not 8 which is required alignment).

user694733
  • 15,208
  • 2
  • 42
  • 68
  • @H2CO3 I haven't got lot of experience with x86. I was mentioning it more like: *"If you are experiencing this problem, then your platform likely has this restriction (what ever your current platform might be)."* – user694733 Nov 27 '13 at 10:13
1

char = 1 byte

double = 8 bytes

int = 4 bytes

align to double =>

padding char => 1+7 

padding double => 8+0

padding int => 4+4

=> 24 bytes

or, simply put, is the multiple of the largest => 3 (the number of fields) * 8 (the size of the largest) = 24

asalic
  • 949
  • 4
  • 10
  • why everywhere you are aligning to double. what were your answer if the double were not there? – gpuguy Nov 27 '13 at 10:01
  • @gpuguy As user694733 well stated in his/her answer, the struct would be misaligned and if aligned by int would be divisible by 4 not by 8. On many platforms the largest of the fields is considered and the rest are aligned to it. Not knowing yours, you can try to redef your struct as {char, int, double} and see if the size after padding is less... but again not sure if it applies to your case – asalic Nov 27 '13 at 10:04
  • 1
    @gpuguy Processors are word oriented. You will need to know the word size of your CPU. From what you describe it is 64 bit. So for the data to be alligned, each must be brought to 64 bits=8 Bytes (assuming 1Byte=8bit on your system, this is surprisingly not always true on all systems) by padding. In this case int which is 4 bytes long on your platform must be padded by 4 bytes. A double is 8 Bytes long so no padding is needed. – H_squared Nov 27 '13 at 10:16
0

my guess would be the size of int in your system is 4 Bytes, so the int must also be padded by 4 Bytes in order to achieve a word size of 8 Bytes.

total_size=sizeof(char) + 7 Byte padding + sizeof(double) + sizeof(int) + 4 Bytes padding = 24 Bytes

H_squared
  • 1,251
  • 2
  • 15
  • 32
0

Good article on padding/alignment: http://www.drdobbs.com/cpp/padding-and-rearranging-structure-member/240007649

Because of the double member it forces everything to be eight byte aligned.

If you want a smaller structure then following structure gives you 16 bytes only!

typedef struct structA 
{
   int         I;
   char        C;
   double      D;
} structA_t;
MichaelMoser
  • 3,172
  • 1
  • 26
  • 26