Is there any guarantee of alignment of address return by C++’s new operation?

The alignment has the following guarantee from the standard (3.7.3.1/2):

The pointer returned shall be suitably aligned so that it can be converted to a
pointer of any complete object type and then used to access the object or array in the
storage allocated (until
the storage is explicitly deallocated by a call to a corresponding deallocation function).

EDIT: Thanks to timday for highlighting a bug in gcc/glibc where the guarantee does not hold.

EDIT 2: Ben’s comment highlights an intersting edge case. The requirements on the allocation routines are for those provided by the standard only. If the application has it’s own version, then there’s no such guarantee on the result.

Leave a Comment