Pointers - Is it safe to point to invalid memory?
Brother Bill
brotherbill at mail.com
Sat Aug 16 15:24:55 UTC 2025
On Saturday, 16 August 2025 at 15:14:21 UTC, H. S. Teoh wrote:
> On Sat, Aug 16, 2025 at 03:03:21PM +0000, Brother Bill via
> Digitalmars-d-learn wrote: [...]
>> I'm not sure we are on the same page.
>> My question is whether having a pointer to invalid memory
>> causes
>> problems. I am fully aware that reading or writing to an
>> invalid
>> memory address causes problems.
>>
>> I am grasping that even having a pointer pointing to an
>> invalid memory address is a huge code smell. But will it
>> cause any undefined behavior merely having a pointer with an
>> invalid memory address.
>
> What do you mean by "cause any undefined behaviour"? UB is
> defined by the language spec, which states that assigning a
> non-pointer value (or a random garbage value) to a pointer is
> UB. What it actually does at runtime is orthogonal to this.
> At the machine level, there is no UB, it's simply following the
> instructions you gave it literally. That the results may not
> be what you expect is a problem at a higher level of
> abstraction.
>
>
So a good D developer should not store an invalid pointer address
into a pointer,
with the single exception of storing a pointer address just past
a slice or array.
More information about the Digitalmars-d-learn
mailing list