What are the pointer aliasing rules in D?
Dmitry Olshansky
dmitry.olsh at gmail.com
Thu Mar 6 16:25:17 UTC 2025
On Thursday, 6 March 2025 at 16:11:37 UTC, Ali Çehreli wrote:
> On 3/6/25 6:34 AM, Paul Backus wrote:
>
> > are allowed to refer to the same memory location. However,
> there are
> > still cases where *dereferencing* those pointers can lead to
> undefined
> > behavior.
>
> This topic came up among colleagues recently. I think such
> restrictions come from CPU architectures where, as I remember
> reading on C++ forums years ago, the mere act of loading a
> misaligned pointer value into a pointer register would cause an
> error (trap?).
ARM is like that, but I think you really must make a load out of
that pointer.
>
> I cast any odd pointer value to any sized integer (and in one
> case to a struct pointer) from a void* buffer area and it works
> on the Intel CPU that I am working on.
As long as pointer is properly aligned for the data in question
eg 4 bytes for int* pointer it should still work.
> Will this work on all or most modern CPUs?
>
> Ali
—
Dmitry Olshansky
dmitry at olshansky.me
https://olshansky.me
More information about the Digitalmars-d-learn
mailing list