C: Difference between revisions

No change in size ,  17 years ago
m
[unchecked revision][unchecked revision]
Line 30:
At some point you might want to compile your code with optimizations enabled. This however can lead to unpredictable results if your C code wasn't written properly. You might end up wasting days or weeks trying to find out why your code works flawlessly without optimizations enabled, but as soon as you compile it with optimizations it just doesn't work anymore or even crashes or locks up your OS. Operating systems are usually multi-threaded environments these days, however the optimizer of compilers generally assume that algorithms and memory changes are single-threaded and so only visible to one instance. So if the optimizer determines that your code does something that is redundant and could be optimized away, it might remove code or change it's logic any way it wants. These are legit changes to the code, but in some instance it's not what you want and it will simply break it. To prevent such things from happening, use the [[volatile (keyword)|volatile]] keyword where neccessary.
 
To fix possible issues due to optimizations performed by processors on runtime (e.g. reordering instructions and memory accesses, caching memory) make sure you use [[atomic operationsoperation]]s or [[memory barriers]] where neccessary. This however isn't an optimization specific to the C/C++ language but rather platform and hardware specific.
 
[[Category:Languages]]
50

edits