|
|
|
|
|
by tialaramex
1414 days ago
|
|
And because C++ says "one past the end" pointers are a thing, both these pointers can exist. As written p is a one-past-the-end pointer into object arr, but the address one past the end of arr may well be the address of x. If pointers are just addresses, these pointers are the same... right? Neither C nor C++ currently actually explain how this works for their "abstract machine" in the standards documents. The reality is that your C++ compilers (and any non-toy C compilers) have pointer provenance because it's a nightmare to optimise C programs without, but since it isn't documented anywhere (my understanding is that C23 might fix this for C by taking a TS and an equivalent fix via P2318 could land in C++ 26) it's difficult to say if you ever find bugs in their behaviour. |
|
While one-past-the-end pointers are allowed to exist, they are not allowed to be dereferenced.
> these pointers are the same... right
The entire point of provenance is that even though their numerical values are the same, they are not the same.
> Neither C nor C++ currently actually explain how this works for their "abstract machine" in the standards documents.
While it isn't mentioned explicitly, it can be inferred from other things that the standard does say. The compiler authors didn't just make it up.