__alignof__(double) answer 8. But actually, it is 4 (that's what gcc generates for record). Apparently the problem happened in gcc 3.0 Release: gcc 3.2.3 Environment: Reading specs from /usr/lib/gcc-lib/i386-linux/3.2.3/specs Configured with: ../src/configure -v --enable-languages=c,c++,java,f77,proto,pascal,objc,ada --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-gxx-include-dir=/usr/include/c++/3.2 --enable-shared --with-system-zlib --enable-nls --without-included-gettext --enable-__cxa_atexit --enable-clocale=gnu --enable-java-gc=boehm --enable-objc-gc i386-linux Thread model: posix gcc version 3.2.3 20030309 (Debian prerelease) Linux thc 2.4.20-k7 #1 Tue Jan 14 00:29:06 EST 2003 i686 unknown unknown GNU/Linux How-To-Repeat: #include <stdio.h> struct bla { char c; double v; }; void main (void) { struct bla b; printf("align double %d\neffective alignement %d\n", __alignof__(double), ((char *)(&b.v)) - ((char *)(&b.c))); }
From: Richard Henderson <rth@redhat.com> To: Frederic De Jaeger <dejaeger@free.fr> Cc: rth@gcc.gnu.org, discuss-gnustep@gnu.org, gcc-bugs@gcc.gnu.org, gcc-prs@gcc.gnu.org, nobody@gcc.gnu.org, richard@brainstorm.co.uk, thoran@free.fr, gcc-gnats@gcc.gnu.org Subject: Re: c/10360: __alignof__(double) answer 8 Date: Wed, 9 Apr 2003 15:40:39 -0700 On Wed, Apr 09, 2003 at 11:59:58PM +0200, Frederic De Jaeger wrote: > Thus, why gcc does not align fields with respect to this *preferred* > alignment? Because the ABI says not to. > How can I compute the address of a field in a record? offsetof. > I need to do this uniformly on all the types. That means I cannot use > the trick : > offset = (char *)&foo.bla - (char *)&foo. > or the "offsetof" macro. Tough luck then. > ... and we expect it to return the alignment used by the compiler (and not > the *preferred* alignment). A meaningless number. Because "the alignment used by the compiler" is going to depend on the context in which it is used. r~
State-Changed-From-To: open->closed State-Changed-Why: Not a bug. __alignof__ returns the *preferred* alignment. You're seeing other SVr4 ABI rules concerning structures.
From: Frederic De Jaeger <dejaeger@free.fr> To: rth@gcc.gnu.org, discuss-gnustep@gnu.org, gcc-bugs@gcc.gnu.org, gcc-prs@gcc.gnu.org, nobody@gcc.gnu.org, richard@brainstorm.co.uk, thoran@free.fr, gcc-gnats@gcc.gnu.org Cc: Subject: Re: c/10360: __alignof__(double) answer 8 Date: 09 Apr 2003 23:59:58 +0200 rth> Synopsis: __alignof__(double) answer 8 rth> State-Changed-From-To: open->closed rth> State-Changed-By: rth rth> State-Changed-When: Wed Apr 9 17:29:53 2003 rth> State-Changed-Why: rth> Not a bug. __alignof__ returns the *preferred* alignment. rth> You're seeing other SVr4 ABI rules concerning structures. Thus, why gcc does not align fields with respect to this *preferred* alignment? How can I compute the address of a field in a record? I need to do this uniformly on all the types. That means I cannot use the trick : offset = (char *)&foo.bla - (char *)&foo. or the "offsetof" macro. because I don't know, statically, the type of the record from which I need to compute offsets. The type information comes from the @encode directive of objective-C (ie: it's a characters string that describe a type). Actually, we are using "objc_alignof_type" (from the objective-c API) and we expect it to return the alignment used by the compiler (and not the *preferred* alignment). Thanks for your help, Frederic De Jaeger
From: Richard Henderson <rth@redhat.com> To: Richard Frith-Macdonald <richard@brainstorm.co.uk> Cc: gcc-gnats@gcc.gnu.org, thoran@free.fr, nobody@gcc.gnu.org, gcc-prs@gcc.gnu.org, gcc-bugs@gcc.gnu.org, discuss-gnustep@gnu.org, rth@gcc.gnu.org, Frederic De Jaeger <dejaeger@free.fr> Subject: Re: c/10360: __alignof__(double) answer 8 Date: Thu, 10 Apr 2003 01:00:00 -0700 On Thu, Apr 10, 2003 at 06:06:44AM +0100, Richard Frith-Macdonald wrote: > It would be interesting to know in what sense the alignment is > 'preferred'. In that it takes one less cycle to load that way? In that that's how data gets aligned which isn't constrained by the ABI? > Shouldn't this either be fixed as Frederic suggests, or the documentation > be changed so that instead of saying 'preferred' it says 'misleading and > largely useless' And indeed, that's what we ALREADY say, except you didn't read it. To wit: Some machines never actually require alignment; they allow reference to any data type even at an odd addresses. For these machines, __alignof__ reports the recommended alignment of a type. r~
From: Richard Frith-Macdonald <richard@brainstorm.co.uk> To: Richard Henderson <rth@redhat.com> Cc: gcc-gnats@gcc.gnu.org, thoran@free.fr, nobody@gcc.gnu.org, gcc-prs@gcc.gnu.org, gcc-bugs@gcc.gnu.org, discuss-gnustep@gnu.org, rth@gcc.gnu.org, Frederic De Jaeger <dejaeger@free.fr> Subject: Re: c/10360: __alignof__(double) answer 8 Date: Thu, 10 Apr 2003 06:06:44 +0100 On Wednesday, April 9, 2003, at 11:40 pm, Richard Henderson wrote: > On Wed, Apr 09, 2003 at 11:59:58PM +0200, Frederic De Jaeger wrote: >> Thus, why gcc does not align fields with respect to this *preferred* >> alignment? > > Because the ABI says not to. It would be interesting to know in what sense the alignment is 'preferred'. Surely if the 'ABI says not to' then the alignment is not preferred? >> ... and we expect it to return the alignment used by the compiler >> (and not >> the *preferred* alignment). > > A meaningless number. Because "the alignment used by the compiler" > is going to depend on the context in which it is used. I think that Frederic means the alignment the compiler uses to lay out structures in memory. It's hard to see how that can be context sensistive since the compiler needs to know it to access the structure elements via a pointer, so the code that stored the structure in memory and the code that retrieves it through the pointer have to use the same alignments. I imagine this is *by far* the most common context in which anyone would want to know a type alignment (actually it's the only one I can think of off hand). So if __alignof__ is not returning the alignment of a type for structure layout, how can it be returning the 'preferred' alignment. Shouldn't this either be fixed as Frederic suggests, or the documentation be changed so that instead of saying 'preferred' it says 'misleading and largely useless' Sorry if this sounds overly aggressive, but I do think that compiler extensions should be useful and behave as expected, and this behavior seems to mean that __alignof__ fails in this.
This was closed as invalid so change it to invalid instead of fixed.
*** Bug 52023 has been marked as a duplicate of this bug. ***
*** Bug 54188 has been marked as a duplicate of this bug. ***
*** Bug 69560 has been marked as a duplicate of this bug. ***
The quoted passage in the documentation now reads --- Some machines never actually require alignment; they allow reference to any data type even at an odd address. For these machines, @code{__alignof__} reports the smallest alignment that GCC gives the data type, usually as mandated by the target ABI. --- which seems clearer that the correct answer here is 4, as specified by the i386 psABI.
On Wed, 11 Apr 2018, jason at gcc dot gnu.org wrote: > The quoted passage in the documentation now reads > > --- > Some machines never actually require alignment; they allow reference to any > data type even at an odd address. For these machines, @code{__alignof__} > reports the smallest alignment that GCC gives the data type, usually as > mandated by the target ABI. That change (r133271) seems incorrect in this context, as it should be the alignment given for complete objects.