This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 115e. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2024-11-11


2532. Kind of pointer value returned by new T[0]

Section: 7.6.2.8  [expr.new]     Status: open     Submitter: Andrey Erokhin     Date: 2022-02-17

A pointer value must have one of the kinds specified in 6.8.4 [basic.compound] paragraph 3:

Every value of pointer type is one of the following:

When allocating an array with no elements, 7.6.2.8 [expr.new] paragraph 10 is silent on the kind of pointer value returned:

When the allocated type is “array of N T” (that is, the noptr-new-declarator syntax is used or the new-type-id or type-id denotes an array type), the new-expression yields a prvalue of type “pointer to T” that points to the initial element (if any) of the array. Otherwise, let T be the allocated type; the new-expression is a prvalue of type “pointer to T” that points to the object created.

Related to that, are p and q allowed to compare equal in the following example?

T *p = new T[0];
T *q = new T;

Some implementations return a pointer past the array cookie for empty arrays, which can compare equal to a pointer to an object obtained from an unrelated allocation. However, if new T[0] is specified to yield a pointer to an object, this behavior violates the rule that pointers to disjoint objects with overlapping lifetimes must not compare equal.