Re: struct calcsize discrepency?



On Dec 4, 3:17 pm, Chris Angelico <ros...@xxxxxxxxx> wrote:
On Mon, Dec 5, 2011 at 1:51 AM, Dave Angel <d...@xxxxxxxxxx> wrote:
In C, the padding to the largest alignment occurs at the
end of a structure as well as between items.  Otherwise, an array of the
struct would not be safely aligned.  if you have an 8byte item followed by a
4 byte item, the total size is 16.

That's padding of the array, not of the structure.

That's a strange way to think of it, especially since the padding also
happens for a single struct object when there's no array present. I
find it cleaner to think of C as having no padding in arrays, but
padding at the end of a struct. See C99 6.7.2.1p15: 'There may be
unnamed padding at the end of a structure or union.' There's no
mention in the standard of padding for arrays.

--
Mark
.



Relevant Pages

  • Re: Does an array of char have alignment issues?
    ... I have no idea what you mean by "tile an array". ... padding bytes after the first char, ... after the last member, ... A union can also ...
    (comp.sys.arm)
  • Re: Malloc Query
    ... the arena is an array even though the program using it does not care. ... so you have to account for that when using a pointer ... the alignment requirement, to allow for arrays... ... Adding one byte of padding would do the trick. ...
    (comp.lang.c)
  • Re: Question about Declare and Type
    ... Jim Carlock wrote: ... In this particular case, no padding is needed within the struct, except ... that if you declare an array of such, each element of the array will ... but one-byte characters when passed to a Declared function -- ...
    (microsoft.public.vb.general.discussion)
  • Re: Malloc Query
    ... the arena is an array even though the program using it does not care. ... Suppose an 'int' has a size and alignment of 4 bytes. ... Adding one byte of padding would do the trick. ...
    (comp.lang.c)
  • Re: [comp.lang.c.moderated] Does "sizeof array" equal "nel * sizeof
    ... - the element type of array is "int", ... The Standard only allows padding in certain places, ... I can't find an actual prohibition. ...
    (comp.lang.c.moderated)