c - Why structure padding is not happening properly? -


#include<stdio.h>  struct {     char        c;     double      e;     int         s; }a;  int main() {     printf("%u\n", sizeof(a));     return 0; } 

it giving output 16. shouldn't 24 if consider structure internal padding , structure padding whole?

i compiling code on ubuntu 14.04 32 bit gcc 4.8.2.

your calculations assume double needs 8-byte aligned. that's not case on architectures.

on 32bit x86 linux gcc, double 4-byte aligned default. can change -malign-double flag make 8-byte aligned.

so layout assuming defaults on 32bit x86 linux:

char       // 1 byte            // 3 byte padding double     // 8 bytes int        // 4 bytes 

so total of 16 bytes, 3 bytes of padding in middle.

the wikipedia article data structure alignment has size/alignment numbers various types on 32bit x86 , 64bit x86_64 in few compilers/environments.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -