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

Re: Programming question: sizeof struct?



%% Jason Gunthorpe <jgg@ualberta.ca> writes:

  >> > For instance, if the structure were:
  >> > 
  >> > struct foo {
  >> >     char text[3];
  >> >     int  num;
  >> > };
  >> > 
  >> > sizeof would return 6 and not 5.  
  >> 
  >> 6? Are you sure you're using Linux/gcc?

  jg> Yes, 6, it will insert a single extra character at the end of text to
  jg> place the alignment of num on a 4 byte boundry.

Yes, so 4 bytes for the array and... how many for the int?  Right :).

  >> > So it's obvious that the compiler is placing a pad byte between
  >> > text and num to align num.  I want it to stop!

  jg> I always hated gcc __attribute__, I prefer the simpler and more common

  jg> #pragma pack(1)
  jg> struct {};
  jg> #pragma pack()

Ugh.  #pragma is terrible.  See the GCC manual for a description of why.

__attribute__ may be verbose but it's _much_ better than #pragma.

-- 
-------------------------------------------------------------------------------
 Paul D. Smith <psmith@baynetworks.com>         Network Management Development
 "Please remain calm...I may be mad, but I am a professional." --Mad Scientist
-------------------------------------------------------------------------------
   These are my opinions---Nortel Networks takes no responsibility for them.


Reply to: