[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: using sizeof



J HU <judowold@hotmail.com> wrote:
> I have declared a structure and I'm using the "sizeof" to get the size of 
> this structure.
> 
> After the call I get that the total size is 64Bytes but if I get the size 
> of each field and I add them manually I get that it should be 61Bytes...
> 
> Anyone knows why the result is not the same?

  61 bytes doesn't fit evenly on a 32-bit boundary; 64 bytes does. Your
struct is padded so that your processor can page through them faster.

  If you really want your struct to take up only the 61 bytes for it's
structure, you can use the "packed" attribute;

  struct foo {
    int a;
    int b;
    int c;
  } __attribute__((__packed__));

  Kinda odd syntax, but it works. :) Really, it's a lot faster for your
processor to deal with structs that start on even 32-bit boundaries, so
only pack the structure if you absolutely need it (eg; to speak a particular
networking/storage protocol...)

	- Tyler



Reply to: