Pointers - Is it safe to point to invalid memory?
H. S. Teoh
hsteoh at qfbox.info
Sat Aug 16 15:14:21 UTC 2025
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.
T
--
Stop staring at me like that! It's offens... no, you'll hurt your eyes!
More information about the Digitalmars-d-learn
mailing list