Assume we have an array that contains N elements of type T.
T a[N];
According to the C++14 Standard, under which conditions do we
In [dcl.array]:
An object of array type contains a contiguously allocated non-empty set of
N
subobjects of typeT
.
Contiguous implies that the offset between any consecutive subobjects of type T
is sizeof(T)
, which implies that the offset of the n
th subobject is n*sizeof(T)
.
The upper bound of n < N
comes from [expr.add]:
When an expression that has integral type is added to or subtracted from a pointer, the result has the type of the pointer operand. If the expression
P
points to elementx[i]
of an array objectx
withn
elements, the expressionsP + J
andJ + P
(whereJ
has the valuej
) point to the (possibly-hypothetical) elementx[i + j]
if0 <= i + j < n
; otherwise, the behavior is undefined.