Pointers - Is it safe to point to invalid memory?

Brother Bill brotherbill at mail.com
Sun Aug 17 01:19:41 UTC 2025


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.


More information about the Digitalmars-d-learn mailing list