This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: interesting struct???????????


On Oct 18, 2000, "Ashok Das" <ashok_s_das@hotmail.com> wrote:

> struct x{
>          int a;
>           char b;
>          };
>   sizeof(struct x) gives 8 why??

It's called padding.  `int's are often more efficiently accessed when
they live in an address that is a multiple of the word size (4); some
machines even won't allow you to load or store words unless the
address is a multiple of the word size.  If you ever create an array
of `struct x', you'd want the `a's to be stored in appropriate
addresses, and sizeof is designed such that:
  sizeof(TYPE[n]) == n * sizeof (TYPE) 

-- 
Alexandre Oliva   Enjoy Guarana', see http://www.ic.unicamp.br/~oliva/
Red Hat GCC Developer                  aoliva@{cygnus.com, redhat.com}
CS PhD student at IC-Unicamp        oliva@{lsd.ic.unicamp.br, gnu.org}
Free Software Evangelist    *Please* write to mailing lists, not to me

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]