logoalt Hacker News

tovejyesterday at 8:42 AM4 repliesview on HN

But that seems obvious. You can't load an integer from an unaligned address.

It's not only C-level is it. There's no (guarantee across architectures for) machine code for that either.


Replies

codefloyesterday at 8:54 AM

> You can't load an integer from an unaligned address.

You can, and the results are machine specific, clearly defined and well-documented. Ancient ARM raises an exception, modern ARM and x86 can do it with a performance penalty. It's only the C or C++ layer that is allowed to translate the code into arbitrary garbage, not the CPU.

show 1 reply
matheusmoreirayesterday at 9:02 AM

Sure you can. In many architectures it works just fine. Works perfectly in x86_64, for example. It's just a little slower.

show 1 reply
mbelyesterday at 8:52 AM

Unless your code targets some exotic architecture, like idk x86.

show 1 reply
pjc50yesterday at 8:54 AM

You missed the point: the pointer existing as a value of that type at all is UB, even if you never try to access anything through it and no corresponding machine code is ever emitted.

show 1 reply