3 ms·
For C to be portable this needs to be undefined behaviour because there are CPUs that don’t support unaligned access
by circuit10 3y ago
For C to be portable this needs to be undefined behaviour because there are CPUs that don’t support unaligned access
- chaboud 3y agoThat’s why, while much of the linked blog is kind of off the mark (signs of someone knowing less than they think they know), the general conclusion, using aligned pointers is recommended, is one that I typically recommend to developers new to C or C++ anyway. I’m alright with folks sticking to aligned pointer operations, largely for performance reasons. On some platforms, unaligned operations are really expensive.
- saagarjha 3y agoI know the author. Your guess here is wrong.
- astrange 3y agoThere are some other reasons, but that's one of them. Another is that you want to guarantee objects are stored aligned in memory because that gives you some free bits in pointers you can hide stuff in. (This has less hardware support than it should.)
- Dylan16807 3y agoIt doesn't need to be undefined to say it crashes on some architectures.
- circuit10 3y agoI guess it could also be implementation defined My point here is that you can’t have “everything works as it does in the native assembly language” and “portable assembly” at the same time because if you rely on implementation defined or undefined behaviour then it’s not portable any more
- Dylan16807 3y agoThat depends on what you mean by "portable". I think being able to use the same code across many platforms is enough to qualify. Being able to access raw machine behavior is part of the premise of portable assembly, not a disqualifier.
- circuit10 3y agoIt’s definitely much more portable than assembly, but code that relies on unaligned pointer accesses won’t work on every platform