http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55727

             Bug #: 55727
           Summary: better support for dynamic allocation of over-aligned
                    types
    Classification: Unclassified
           Product: gcc
           Version: 4.8.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
        AssignedTo: unassig...@gcc.gnu.org
        ReportedBy: kr...@kde.org


Created attachment 28991
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=28991
simple testcase for new

Hello,

I just tested the alignas support in GCC 4.8 and wondered how far the C++11
spec defines behavior of dynamic allocation of over-aligned types. Because,
with GCC 4.8, dynamic allocation via new or std::allocator (e.g. in
std::vector) results in unaligned placement. I quote the relevant parts of the
spec below, because I believe GCC could be much more helpful to developers.

I believe a must-have is that if dynamic allocation of an over-aligned type
would result in misalignment, the allocatio fails (3.11.9, 17.6.3.5.6). Perfect
would be a compile-time failure, which is theoretically possible. Runtime
errors resulting from misaligned allocation are often hard to debug, therefore
it would be *very* helpful to get an early warning about misalignment.

As I understand it, the C++ spec allows for GCC to also just do the right thing
automatically, which would be my preferred solution. So when I'd do:
struct alignas(128) Foo { int x; };
Foo *f = new Foo;
I'd (per default) get a pointer f that is aligned as requested (without having
to overload new/delete). The same would be very important for std::vector<Foo>.
Right now it does not even suffice to reimplement new/delete inside Foo to make
std::vector<Foo> work.

C++11 spec quotes:

3.9.2.3 "Pointers to over-aligned types (3.11) have no special representation,
but their range of valid values is restricted by the extended alignment
requirement. This International Standard specifies only two ways of obtaining
such a pointer: taking the address of a valid object with an over-aligned type,
and using one of the runtime pointer alignment functions. An implementation may
provide other means of obtaining a valid pointer value for an over-aligned
type.

3.11.9 "a request for runtime allocation of dynamic storage for which the
requested alignment cannot be honored shall be treated as an allocation
failure"

5.3.4.1 "It is implementation-defined whether over-aligned types are supported
(3.11)."

5.3.4.14 "The block of storage is assumed to be appropriately aligned and of
the requested size."

17.6.3.5.6 "If the alignment associated with a specific over-aligned type is
not supported by an allocator, instantiation of the allocator for that type may
fail. The allocator also may silently ignore the requested alignment. [ Note:
Additionally, the member function allocate for that type may fail by throwing
an object of type std::bad_alloc. — end note"

20.6.9.1.5 "Returns: A pointer to the initial element of an array of storage of
size n * sizeof(T), aligned appropriately for objects of type T. It is
implementation-defined whether over-aligned types are supported (3.11)."

Reply via email to