What are the pointer aliasing rules in D?
Paul Backus
snarwin at gmail.com
Thu Mar 6 14:34:26 UTC 2025
On Thursday, 6 March 2025 at 10:46:20 UTC, tmp wrote:
> I come from a C/C++ background where the pointer aliasing rules
> make an assumption that pointers to different types cannot
> alias each other (with the exception of a pointer to char).
> This is known as the strict aliasing rule and while it can
> increase optimizations it makes it quite hard to write some low
> level code such as allocators or writing efficient memory
> copying functions.
>
> Do the same rules exist in D? If they do, do workarounds exist?
No, D does not have strict aliasing rules. Pointers of different
types are allowed to refer to the same memory location. However,
there are still cases where *dereferencing* those pointers can
lead to undefined behavior.
Some relevant sections of the language spec:
* [Memory Model][1]
* [Pointers][2]
* [Type Qualifiers][3]
[1]: https://dlang.org/spec/intro.html#memory-model
[2]: https://dlang.org/spec/type.html#pointers
[3]: https://dlang.org/spec/const3.html
More information about the Digitalmars-d-learn
mailing list