3 ms·
I have at times (ab)used volatile to aid in debugging sessions, something like volatile bool doCheck = false; if (doCheck) { // code I want to enabl
by MauranKilom 5y ago
I have at times (ab)used volatile to aid in debugging sessions, something like
volatile bool doCheck = false;
if (doCheck)
{
// code I want to enable at some point during debugging
}
The idea is that I attach a debugger, and then only at a certain point enable doCheck.
I was baffled to learn that MSVC will happily constant-fold the false into the if, as long as the variable is function-local. The variable still exists and I can change it in the debugger, but it doesn't actually impact control flow as intended. The "solution" is to move it to e.g. global scope (this is a debugging hack, remember).
Not an exact match for what you asked, but I think a good reminder that optimizers work in mysterious ways, and sprinkling in volatile may confuse the programmer more than the optimizer...