Hacker News new | ask | show | jobs
by circuit10 1033 days ago
For C to be portable this needs to be undefined behaviour because there are CPUs that don’t support unaligned access
3 comments

That’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.

I know the author. Your guess here is wrong.
There 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.)

It doesn't need to be undefined to say it crashes on some architectures.
I 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

That 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.
It’s definitely much more portable than assembly, but code that relies on unaligned pointer accesses won’t work on every platform