TITLE: padding, alignment, and linking (Newsgroups: comp.std.c++, 4 Nov 97) [ This is part of a discussion of padding and alignment. For those of you unfamiliar with this, some architectures, for example, require that "words" fall on 4 byte boundaries. This is usually for hardware memory accessing efficiencies. When you declare structs or classes, the compiler is free to insert hidden bytes (ie, padding) between your struct's data members for better alignment. -adc ] ----- KANZE: (31 Oct 97) |> >The allowed padding is to ensure "appropriate" alignment. The |> >implementation is free to decide whatever it wants with regards to what |> >is appropriate -- for example, an implementation might decide that all |> >structure elements are aligned on 4 byte boundaries. |> > |> >Also, the implementation is free to put the padding whereever it likes |> >(except at the beginning). Thus, assuming 4 byte alignment requirement |> >for int's, consider the following: |> > |> > struct X { char c1 ; char c2 ; int i ; } ; |> > |> >It is obvious that the implementation must put two bytes of padding |> >somewhere before i. Whether these bytes are before c2, after c2, or one |> >before, one after, is up to the implementation. ----- [snip] UNKNOWN: |> >I am against any constraits on the layout; I'd like to |> >see the restriction that addresses of members are ordered |> >removed). |> > |> >struct T { int a, b; } x; |> >assert (&x.a < &x.b); // shouldn't be guarantied |> PARKER: bparker@mailbox.uq.edu.au (Brian Parker) writes: |> Perhaps, but the fact remains that it is the intent of the current |> standard to completely determine the alignment layout- certainly that |> was made quite clear in the C++ Report article I cited- and so [ The article was "Standard Update: The memory model-type representations" Josee Lajoie, C++ Report, May 96. ] |> [class.mem]/12 needs to be fixed. I don't believe that it was ever |> intended to allow leeway in distributing alignment padding, and if |> that truly is the intent then *that* should be clarified in |> [class.mem]/12. KANZE: James Kanze It was certainly the intent of the C standard. An implementation for Intel could choose space (no padding) or time (padding for alignment) optimization, according to what it felt was appropriate. (At least some implementations made this a compile time option. THAT must have led to some interesting link errors:-).)