The problem is that you have:
uint16_t x;
but then you try to write to this memory location, as if it were an unsigned place.
If you were on a system where unsigned and uint16_t are the same type, this is normal. But on other systems, such as the one you used for your sample code, you have problems.
First of all, it causes undefined behavior, violating the rule of strict aliases . Variables of type uint16_t can only be written via lvalues ββof type uint16_t or character type.
But even if this does not violate a strict alias, you will still call UB, writing outside x . Perhaps you are writing 4 or 8 bytes in a 2-byte memory location, so it will overflow the buffer.
It may also be UB if x is incorrectly aligned for unsigned .
source share