Remix.run Logo
runeks 4 hours ago

Wouldn't the compiler take care of producing the correct machine code?

octachron 4 hours ago | parent | next [-]

The issue is that the C memory model allows more behaviours than the memory model of x86-64 processors. You can thus write code which is incorrect according to the C language specification but will happen to work on x86-64 processors. Moving to arm64 (with its weaker memory model than x86-64) will then reveal the latent bug in your program.

Someone 3 hours ago | parent [-]

And “happen to work on x86-64 processors” also will depend on the compiler. If you write

  *a = 1;
  *b = 'p';
both the compiler and the CPU can freely pick the order in which those two happen (or even execute them in parallel, or do half of one first, then the other, then the other half of the first, but I think those are hypothetical cases)

x86-64 will never do such a swap, but x86-64 compilers might.

If you write

  *a = 1;
  *b = 2;
, things might be different for the C compiler because a and b can alias. The hardware still is free to change that order, though.
mrweasel 3 hours ago | parent | prev | next [-]

OpenBSD famously keeps a lot of esoteric platforms around, because running the same code on multiple architectures reveal a lot of bugs. At least that was one of the arguments previously.

mhh__ 4 hours ago | parent | prev [-]

The compiler relies on the language and programmer to enforce and follow a memory consistency model