2 ms·
> If I just want speed and don’t care about safety at all, I’d be using assembly, right? Not really. C is a reasonable choice of language for high-performance
by MaxBarraclough 8y ago
> If I just want speed and don’t care about safety at all, I’d be using assembly, right?
Not really. C is a reasonable choice of language for high-performance code. There may be instances where your hand-written assembly can outperform compiled C, but it's far from guaranteed, and of course even if you did have such a guarantee, it might not be worth the productivity impact.
Even performance-critical codebases like the Unreal Engine don't use much assembly, as far as I know.
> Speed is definitely an important part of the story but it’s by no means the only part.
Agree.
> Something like FORTRAN maybe? No pointers, as I understand it, hence no aliasing problems.
Indeed, but my understanding is that the performance wins there aren't exactly night-and-day. C is pretty close to as fast as it gets.
> Right, what I’m getting at is, the compiler is also free not to add a check, and most compilers do not in fact add a check, because they care more about speed.
I was unclear - I meant the compiler is permitted to add a check and then use a special optimised path in the case that the pointers do not alias.
It’s quite legal to pass aliasing pointers. When compiling a function which has two pointer-to-int parameters, the compiler is not permitted to assume that they do not alias, unless you use the 'restrict' keyword (which is rarely seen as it's so risky).
> aliased pointers of different types can lead to undefined behavior
Correct (with the special exceptions of void* and char*). Because you shouldn't be doing that.