Title
Kind of pointer value returned by new T[0]
Status
open
Section
7.6.2.8 [expr.new]
Submitter
Andrey Erokhin

Created on 2022-02-17.00:00:00 last changed 34 months ago

Messages

Date: 2022-02-17.00:00:00

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:
  • a pointer to an object or function (the pointer is said to point to the object or function), or
  • a pointer past the end of an object (7.6.6 [expr.add]), or
  • the null pointer value for that type, or
  • an invalid pointer value.

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.

History
Date User Action Args
2022-02-17 00:00:00admincreate