Can I pass a pointer to before the beginning of an array if I know only existing elements will be used?

The subtraction has undefined behaviour.

[expr.add]:

If the expression P points to element x[i] of an array object x with n elements, [...] the expression P - J points to the (possibly-hypothetical) element x[i − j] if 0 ≤ i − j ≤ n; otherwise, the behavior is undefined.

Note that the act of producing the value is undefined in itself - you don't even need to use the result.


No you can't.

Pointer arithmetic is only valid within arrays, with the exception that you can set a pointer to point one past the final element of an array, and for this purpose an object is considered to be a single element array. The behavior on reading such an out of range pointer (let alone dereferencing it) is undefined.

Can't you simply pass the array along with an offset (perhaps of type std::ptrdiff_t)?