5

Can anyone please tell me how come the size of the structure shown below is 24 and not 20.

typedef struct
{
    double d;  // this would be 8 bytes
    char c;   // This should be 4 bytes considering 3 bytes padding
    int a;   // This would be 4 bytes
    float b; // This would be 4 bytes
} abc_t;

main()
{
    abc_t temp;
    printf("The size of struct is %d\n",sizeof(temp));
}

My asumption is that the size of structure would be 20 when we consider padding but when i run this code the size is printing as 24.

haccks
  • 104,019
  • 25
  • 176
  • 264

2 Answers2

6

Size would be 24. It is because the last member is padded with the number of bytes required so that the total size of the structure should be a multiple of the largest alignment of any structure member.

So padding would be like

typedef struct
{
    double d;  // This would be 8 bytes
    char c;    // This should be 4 bytes considering 3 bytes padding
    int a;     // This would be 4 bytes
    float b;   // Last member of structure. Largest alignment is 8.  
               // This would be 8 bytes to make the size multiple of 8 
} abc_t;

Read the wiki article for more detail.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • Thanks a lot. Can i post another simple question to you? – user2520451 Oct 01 '16 at 12:56
  • a function pointer int (*fp)(int , int). When i print the size of fp, it is giving as 8 where i assume that any function pointer or a structure pointer would be 4 on a 32 bit machine.? – user2520451 Oct 01 '16 at 13:03
  • @user2520451; I am not quite sure. There is no guarantee that the size of both pointers can be same. Alignment of function pointer is different than that of structure pointers. – haccks Oct 01 '16 at 13:34
  • Section §6.2.5/28 of C11 draft says: *A pointer to `void` shall have the same representation and alignment requirements as a pointer to a character type.48) Similarly, pointers to qualified or unqualified versions of compatible types shall have the same representation and alignment requirements. All pointers to structure types shall have the same representation and alignment requirements as each other. All pointers to union types shall have the same representation and alignment requirements as each other. Pointers to other types need not have the same representation or alignment requirements.* – haccks Oct 01 '16 at 13:34
-1

Maybe packed attribute will answer question.

typedef struct
{
    double d;  // this would be 8 bytes
    char c;   // This should be 4 bytes considering 3 bytes padding
    int a;   // This would be 4 bytes
    float b; // This would be 4 bytes
} __attribute__((packed)) abc_t ;
Denis Beraldo
  • 19
  • 1
  • 3
  • No, it does not correctly answer the question. Try it: is your sizeof now 20, as OP was expecting? – Jongware Oct 01 '16 at 18:09
  • So I gave you the benefit of the doubt and tried. Result: `sizeof abc_t is 17`, which *is* correct for `packed` but not the `20` that OP expected. – Jongware Oct 02 '16 at 11:11