Sizeof structure not expected in C since compiler does not add padding

First I tried to go through existing threads in stackoverflow regarding my question. At least I could not find a thread that talks about my problem. I am executing the following code for a 32-bit machine through

gcc -m32 -o size32 size.c

On the contrary, I believe that the compiler does not add an extra complement. I did not add a flag. Therefore, I expect the compiler to add extra bytes. Here is my problem.

struct s{
    char c;
    int i;
    double d;
    void *p;
};

    struct s temp;
    void *q;
    double d1=10;

    printf("size of struct = %d sizeof q = %d sizeof double = %d\n",sizeof(temp),sizeof(q),sizeof(d1));

The output was size struct = 20 sizeof q = 4 sizeof double = 8

. char (1 ) + 3 padding + int (4 ) + double (8 ) + (void *) (4 ), 20 4 - ( double - 8 , 8 ), , , 24 . , 24 . 20 ?

CHID

+4
1

Linux, -malign-double , 4 , .

. .

+4

All Articles