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

2024-05-06


2875. Missing support for round-tripping nullptr through indirection/address operators

Section: C.7.4  [diff.expr]     Status: open     Submitter: Richard Smith     Date: 2024-03-21

C supports the following, C++ does not (see issues 232 and 2823):

  void f() {
    char *p = nullptr;
    char *p2 = &*p;       // OK in C, undefined behavior in C++
    int a[5];
    int *q = &a[5];       // OK in C, undefined behavior in C++
  }

This incompatibility should be documented in Annex C.

Possible resolution:

Add a new paragraph to C.7.4 [diff.expr] as follows:

Affected subclause: 7.6.2.2 [expr.unary.op]
Change: Taking the address of a dereferenced null or past-the-end pointer value is well-defined in C (and yields the original pointer value), but results in undefined behavior in C++. For example:
  void f() {
    char *p = nullptr;
    char *p2 = &*p;   // well-defined in C, undefined behavior in C++
    char *p3 = &p[0]; // well-defined in C, undefined behavior in C++
    int a[5];
    int *q = &a[5];   // well-defined in C, undefined behavior in C++
  }
Rationale: Consistent treatment of lvalues in C++.
Effect on original feature: Well-formed and well-defined C code exhibits undefined behavior in C++.
Difficulty of converting: Syntactic transformation to pointer arithmetic and possible addition of a check for null pointer values.
How widely used: Occasionally.