On Saturday, 16 August 2025 at 23:42:04 UTC, 0xEAB wrote:
On Saturday, 16 August 2025 at 15:44:45 UTC, Richard (Rikki)
Andrew Cattermole wrote:
Source: Programming in D book, page 432, chapter 68.8
Quote: It is valid to point at the imaginary element one past
the end of an array.
That is not the spec, the book is wrong.
IMHO the book is *not* wrong but rather vague.
What — I think — the book tries to convey is that you can
create a zero-length slice outside of the “range” of the
original slice.
Separation of concerns:
1. Other than null, is it within spec to have a pointer pointing
to invalid memory.
I believe the answer is yes.
A pointer can point to any random address, even to memory
addresses that don't exist.
Merely pointing to an invalid address will not produce UB
(undefined behavior)
It would definitely be a "code smell".
2. Is it within spec to dereference a pointer pointing to invalid
memory?
Dereferencing null is not valid.
Dereferencing a pointer pointing to invalid memory in not
valid and can result in UB.