4 ms·
Compilers can only optimize away loops if they know that the loop has no side effects. Compilers like GCC have to have a list of standard functions that they kn
by danellis 10y ago
Compilers can only optimize away loops if they know that the loop has no side effects. Compilers like GCC have to have a list of standard functions that they know are pure.
This can trip you up sometimes. For example, if you do something like use memset to zero out sensitive data when you're done using it, the computer can say "the result of memset is never used, and the reference to that memory is lost, so there's no need to actually make the call" and you're left with secrets in memory.
- saagarjha 10y agoForgive me if I don't know what I'm talking about here, but could volatile be used here to force the compiler to perform the memset?
- HorizonXP 10y agoYou're correct. (https://barrgroup.com/Embedded-Systems/How-To/C-Volatile-Keyword https://barrgroup.com/Embedded-Systems/How-To/C-Volatile-Key...) But again, you're telling the compiler that you know what you're doing, and to not perform its normal optimization. I'm not well-versed in compiler design, but I think that's the tradeoff you have to make between trusting the user's design choices, or not.