Bug 65037 - Ambiguous behaviour of __alignof__ built-in operator
Summary: Ambiguous behaviour of __alignof__ built-in operator
Keywords:
Status: CLOSED NOTABUG
Alias: None
Product: Red Hat Linux
Classification: Retired
Component: gcc3
Version: 7.2
Hardware: All
OS: Linux
medium
high
Target Milestone: ---
Assignee: Jakub Jelinek
QA Contact:
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2002-05-16 13:56 UTC by Grigory Zagorodnev
Modified: 2016-10-03 20:50 UTC (History)
0 users

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Clone Of:
Environment:
Last Closed: 2004-10-02 20:09:16 UTC
Embargoed:


Attachments (Terms of Use)


Links
System ID Private Priority Status Summary Last Updated
oVirt gerrit 65037 0 master MERGED resourceManager: Get rid of getInstance 2016-10-03 20:50:47 UTC

Description Grigory Zagorodnev 2002-05-16 13:56:35 UTC
From Bugzilla Helper:
User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; NetCaptor 
6.5.0PB8)

Description of problem:
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?


Version-Release number of selected component (if applicable):


How reproducible:
Always

Steps to Reproduce:
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


Additional info:

Comment 1 Jakub Jelinek 2002-05-16 14:16:08 UTC
This is because of IA-32 ABI which requires aggregate fields bigger than
4 bytes to be only 4 bytes aligned (unless you use -malign-double, which of
course breaks the ABI).
__alignof__ (double) == 8 is correct, likewise offsetof.
__alignof__ (A::d) should probably change, but that's certainly a 3.2 material.

Comment 2 Richard Henderson 2004-10-02 20:09:16 UTC
__alignof__ has always been documented as the *recommended* alignment
for the type.  I don't see that this is a bug at all.


Note You need to log in before you can comment on or make changes to this bug.