Bug 65037 - Ambiguous behaviour of __alignof__ built-in operator
Ambiguous behaviour of __alignof__ built-in operator
Product: Red Hat Linux
Classification: Retired
Component: gcc3 (Show other bugs)
All Linux
medium Severity high
: ---
: ---
Assigned To: Jakub Jelinek
Depends On:
  Show dependency treegraph
Reported: 2002-05-16 09:56 EDT by Grigory Zagorodnev
Modified: 2016-10-03 16:50 EDT (History)
0 users

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Last Closed: 2004-10-02 16:09:16 EDT
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---

Attachments (Terms of Use)

External Trackers
Tracker ID Priority Status Summary Last Updated
oVirt gerrit 65037 master MERGED resourceManager: Get rid of getInstance 2016-10-03 16:50 EDT

  None (edit)
Description Grigory Zagorodnev 2002-05-16 09:56:35 EDT
From Bugzilla Helper:
User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; NetCaptor 

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

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 10:16:08 EDT
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 16:09:16 EDT
__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.