This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
alignof () broken in gcc 3.2 x86
- From: Martin Buchholz <martin at xemacs dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Tue, 19 Nov 2002 23:47:41 -0800
- Subject: alignof () broken in gcc 3.2 x86
- Reply-to: martin at xemacs dot org
gcc 3.2 on Linux x86 claims
alignof (double) == 8
alignof (long long) == 8
whereas gcc 2.95.3 claims
alignof (double) == 4
alignof (long long) == 4
Furthermore, gcc 3.2 lays out objects as though the alignment were
still 4 -- that is, alignof appears to return the wrong numbers.
The test program bolow includes my own implementation of alignof, that
agrees with the 2.95.3 implementation of alignof.
Self-explanatory shell transcript follows:
(martin@wobble) ~/src $ cat alignof.cc
#include <iostream>
using namespace std;
template <class T> struct alignof;
template <class T, int sizeof_quantum>
struct alignof_helper
{
enum { RET = sizeof_quantum };
};
template <class T>
struct alignof_helper<T,0>
{
enum { RET = alignof<T>::RET };
};
template <class T>
struct alignof
{
struct bigger { T x; char c; };
enum { RET = alignof_helper<bigger, (sizeof (bigger) - sizeof (T))>::RET };
};
#define ALIGNOF(type) alignof<type>::RET
int
main (int argc, char *argv[])
{
#define PRINT_ALIGNOF(type) cout << "ALIGNOF(" #type ") = " << ALIGNOF(type) << ' ' << __alignof__(type) << endl
PRINT_ALIGNOF(char);
PRINT_ALIGNOF(short);
PRINT_ALIGNOF(int);
PRINT_ALIGNOF(long);
PRINT_ALIGNOF(long long);
PRINT_ALIGNOF(float);
PRINT_ALIGNOF(double);
PRINT_ALIGNOF(long double);
return 0;
}
(martin@wobble) ~/src $ g++ -Wall alignof.cc && ./a.out
ALIGNOF(char) = 1 1
ALIGNOF(short) = 2 2
ALIGNOF(int) = 4 4
ALIGNOF(long) = 4 4
ALIGNOF(long long) = 4 8
ALIGNOF(float) = 4 4
ALIGNOF(double) = 4 8
ALIGNOF(long double) = 4 4
(martin@wobble) ~/src $ /usr/bin/g++ -Wall alignof.cc && ./a.out
ALIGNOF(char) = 1 1
ALIGNOF(short) = 2 2
ALIGNOF(int) = 4 4
ALIGNOF(long) = 4 4
ALIGNOF(long long) = 4 4
ALIGNOF(float) = 4 4
ALIGNOF(double) = 4 4
ALIGNOF(long double) = 4 4
(martin@wobble) ~/src $ g++ --version
g++ (GCC) 3.2
Copyright (C) 2002 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
(martin@wobble) ~/src $ /usr/bin/g++ --version
2.95.3
(martin@wobble) ~/src $ uname -a
Linux wobble 2.4.10-4GB #1 Fri Sep 28 17:20:21 GMT 2001 i586 unknown