4 ms·
>> this code would work everywhere > it does not, it will be miscompiled by modern compilers. Sorry, due to formatting bug I overlooked this. Can you show me
by ordu 10y ago
>> this code would work everywhere
> it does not, it will be miscompiled by modern compilers.
Sorry, due to formatting bug I overlooked this.
Can you show me example of such a modern compiler? I suspect that you mean some C++ compiler, and they probably do, they would `miscompile' my example, because they treat struct in a matter similar to a class with vtable and all other stuff. But we are speaking about C, not C++. But if I'm mistaken with my suggestions, I'd like to know about modern compiler of C which prove me wrong. Such a proof can help me to understand modern C much better.
- gpderetta 10y agoIt is hard for compilers to miscompile this specific example as it doesn't do much at all. The idea is that a write to pfoo[1] couldn't possibly alias with any write to foo, so the compiler should be free to reorder accesses if profitable. This is the same in C and C++ and has nothing go do with vtables. For what is worth, I couldn't get gcc, clang and icc it to miscompile [¹] a slightly changed example, so either it is not actually UB or compilers still refrain to make this kind of optimization as it would break way too much code. [¹] i.e. they elect to reload from the struct after writing to the array and vice versa even when it would be profitable not to do so.