5 ms·
It's definitely rife with footguns but you can very much write aliasing-safe systems code (or even kernel code, for that matter). Or at the very least you shoul
by simias 5y ago
It's definitely rife with footguns but you can very much write aliasing-safe systems code (or even kernel code, for that matter). Or at the very least you should strive to contain the alias-infringing code in well delimited sections of the source code that are built with special flags for instance.
If optimizations break code due to aliasing violations I would really recommend fixing the code, not turning the optimizations off!
Also if anything C's aliasing is less strict than Fortran by default, hence the later introduction of "restrict" to allow further optimizations.
- matheusmoreira 5y ago> you can very much write aliasing-safe systems code (or even kernel code, for that matter) Yeah, sometimes it's possible. Usually by making a mess of everything with unions. If I remember correctly, type punning with unions is still illegal C code but in practice every compiler understands the idiom. The simple and intuitive solution to many problems is to cast the data to the new pointer and work directly with it. This should always produce correct code no matter what. People think like this and they write code with these assumptions in mind. In practice, nobody really cares too much what the C standard says. What matters is whether the compilers produce the desired code.
- deleted 5y ago[deleted]
- saagarjha 5y agoPinning via unions is legal in C. Casting via a pointer is not and may produce incorrect code.