public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* c++/6829: Ambiguous behaviour of __alignof__ built-in operator
@ 2002-05-27  3:26 Grigory_Zagorodnev
  0 siblings, 0 replies; 2+ messages in thread
From: Grigory_Zagorodnev @ 2002-05-27  3:26 UTC (permalink / raw)
  To: gcc-gnats


>Number:         6829
>Category:       c++
>Synopsis:       Ambiguous behaviour of __alignof__ built-in operator
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Mon May 27 03:26:00 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator:     Grigory Zagorodnev
>Release:        3.1
>Organization:
>Environment:
host: i686-pc-linux-gnu
build: i686-pc-linux-gnu
target: i686-pc-linux-gnu
configured with: ./configure
>Description:
There is something wrong with gcc's __alignof__ builtin operator.
For "double" type it returns 8. But looking at the class layout we see that double is aligned to 4.
The situation becomes more strange is we check alignment for "long double" type - it's 4!

Following testcase shows the problem with __alignof__ (double)...

----- testcase (fail.cpp) ---------------------------------------------
#include <stdio.h>

class A {
public:
    int		i;
    double 	d;
} a;

int main(){
    printf("alignof(double) = %d\n", __alignof__(double));
    printf("alignof(A::d)   = %d\n", __alignof__(a.d));
    printf("offsetof(A, d)  = %d\n", (char *)&a.d - (char *) &a);
    return 0;
}

----- testcase output --------------------------------------------------
alignof(double) = 8
alignof(A::d)   = 8
offsetof(A, d)  = 4

So, what is going on here?
If we really have double aligned to 8 bytes, we should get offset 8 within the class.
If we have offset 4 bytes, does it mean that alignment is not greater then 4?
>How-To-Repeat:
1. compile testcase 
    g++3 -c fail.cpp

2. Run to see the output


Actual Results:  
alignof(double) = 8
alignof(A::d)   = 8
offsetof(A, d)  = 4


Expected Results:  
number should be equal for all cases
>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: c++/6829: Ambiguous behaviour of __alignof__ built-in operator
@ 2002-05-28 11:08 rth
  0 siblings, 0 replies; 2+ messages in thread
From: rth @ 2002-05-28 11:08 UTC (permalink / raw)
  To: Grigory_Zagorodnev, gcc-bugs, gcc-prs, nobody

Synopsis: Ambiguous behaviour of __alignof__ built-in operator

State-Changed-From-To: open->closed
State-Changed-By: rth
State-Changed-When: Tue May 28 11:00:06 2002
State-Changed-Why:
    Not a bug.  __alignof__ is not expressive enough to cover all
    situations.  It is documented as the *preferred* alignment.
    
    The i386 svr4 abi (which linux follows) caps alignment of
    structure members at 4.  So if you put a double in memory,
    gcc will align to 8 (because it doesn't affect the abi),
    whereas within a structure we are constrained to the 
    narrower alignment.

http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=6829


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2002-05-28 18:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-05-27  3:26 c++/6829: Ambiguous behaviour of __alignof__ built-in operator Grigory_Zagorodnev
2002-05-28 11:08 rth

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).