Bug 65037 - Ambiguous behaviour of __alignof__ built-in operator
Summary: Ambiguous behaviour of __alignof__ built-in operator
Alias: None
Product: Red Hat Linux
Classification: Retired
Component: gcc3 (Show other bugs)
(Show other bugs)
Version: 7.2
Hardware: All Linux
Target Milestone: ---
Assignee: Jakub Jelinek
QA Contact:
Depends On:
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:
Story Points: ---
Clone Of:
Last Closed: 2004-10-02 20:09:16 UTC
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 20:50 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 

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