On Thu, Mar 30, 2006 at 01:03:15AM +0200, Joerg Schilling wrote:
>Steve Bennett <[EMAIL PROTECTED]> wrote:
>
>> > After reading the C-99 Standard, it turned out that the last method 
>> > is the "official" way to prevent tail padding of structures.
>> > 
>> > Could you please run the last test again but use:
>> > 
>> > #pragma pack(1)
>> > 
>> > struct test1
>> > {
>> >     char blerg[1];
>> >     char type[4];
>> >     char flibble[3];
>> >     char more[2];
>> >     char dummy[];
>> > };
>> > 
>>
>> sizeof(test1) is 12
>>
>> And just in case you are wondering, I ran all those previous tests again
>> with -std=c99 with exactly the same results.
>
>Thank you for the test!
>
>So GCC is in clear violation with the C-99 standard.

Which part of the C-99 standard, exactly? Section 6.7.2.1, paragraph
#12,#14,#15 (see draft N869 from
http://cbfalconer.home.att.net/download/n869_txt.bz2) says the
following:

---------------------------------------

  [#12]  Within  a structure object, the non-bit-field members
  and the units in which bit-fields reside have addresses that
  increase in the order in which they are declared.  A pointer
  to a structure object, suitably  converted,  points  to  its
  initial  member  (or  if that member is a bit-field, then to
  the unit in which it resides), and vice versa.  There may be
  unnamed  padding  within  a structure object, but not at its
  beginning.

  ...

  [#14] There may be unnamed padding at the end of a structure
  or union.

  [#15] As a special case, the last  element  of  a  structure
  with more than one named member may have an incomplete array
  type.  This is called a flexible array member, and the  size
  of  the  structure  shall be equal to the offset of the last
  element of an otherwise identical  structure  that  replaces
  the  flexible  array  member  with  an  array of unspecified
  length.97)  When an lvalue whose type is a structure with  a
  flexible  array  member  is  used  to  access  an object, it
  behaves as if that member were  replaced  with  the  longest
  array,  with  the same element type, that would not make the
  structure larger than the object being accessed; the  offset
  of the array shall remain that of the flexible array member,
  even if this would  differ  from  that  of  the  replacement
  array.   If  this  array  would  have  no  elements, then it
  behaves as if it  had  one  element,  but  the  behavior  is
  undefined  if  any attempt is made to access that element or
  to generate a pointer one past it.

  ____________________

  97)The length is unspecified to  allow  for  the  fact  that
     implementations   may   give   array   members  different
     alignments according to their lengths.

---------------------------------------

i.e. there may be no padding before the first member of a structure,
but there may be elsewhere within the structure including right at the
end. As a special case, if you specify the last member of your
structure as a "flexible array member" then there will be no padding
_at the end of the structure_. It does not affect padding _within_ the
structure.

I'm sorry, I don't see anything here to support your assertion that
gcc is behaving incorrectly.

-- 
Steve McIntyre, Cambridge, UK.                                [EMAIL PROTECTED]
"Since phone messaging became popular, the young generation has lost the
 ability to read or write anything that is longer than one hundred and sixty
 characters."  -- Ignatios Souvatzis



-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to