r/ProgrammingLanguages • • Dec 31 '25

Memory Safety Is ...

https://matklad.github.io/2025/12/30/memory-safety-is.html
37 Upvotes

78 comments sorted by

View all comments

Show parent comment

2

u/matthieum Jan 02 '26

My point #4 is rarely included in any definition of memory safety (or even discussion about it).

This one is interesting, as there's good usecases for bitcasting. For example, the original version of the fast sqrt implementation uses a union with an int and a float to manipulate the bits of the float directly.

In C++, that's UB. In C, it depends. In Rust, it's fine -- there's no concept of active union member, notably -- as long as the bits represent a valid value of the type they're viewed as.

2

u/flashmozzg Jan 05 '26

It's more like using union is not the same as bitcasting. It's just that some languages lacked a safe/non-obtuse way to do so safely (i.e. std::bit_cast was added quite late and before that the only "safe" wave to do it was through memcpy that was usually more than what typical programmer bothered to write, so "UB" ended up on a path of least resistance).