This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

c++/6829: Ambiguous behaviour of __alignof__ built-in operator



>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:


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]