Skip to content

CWG2719 [expr.new] Is placement-newing an object at an unaligned address undefined behavior? #291

Closed
cplusplus/draft
#6328
@frederick-vs-ja

Description

@frederick-vs-ja

Full name of submitter (unless configured in github; will be published with the issue): Jiang An

Reference (section label): [expr.new], [basic.life]

Link to reflector thread (if any):

Issue description:
It seems that nothing in [expr.new] says that non-allocating placement new at an unaligned address has undefined behavior. [basic.life] p1.1 may be related, but one may think that such a new-expression just fails to create an object while being well-defined (however, the value of the new-expression seems underspecified if so).

For example, assuming alignof(double) == 8.

#include <new>

int main() {
  alignas(double) unsigned char buf[sizeof(double) + 1];
  new ((void*)(buf + 1)) double{};
}

Does the above program have undefined behavior? Or is the behavior well-defined despite that the new-expression doesn't create a double object?

It seems that the behavior should be undefined, because it generally requests unaligned access.

Suggested resolution:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions