简体   繁体   English

什么是最简单的“未使用”代码,不会被优化?

[英]What's the simplest “unused” code that won't be optimized out?

Very frequently, I want to test the value of a variable in a function via a breakpoint. 我经常想通过断点测试函数中变量的值。 In many cases, that variable never actually gets referenced by any code that "does stuff", but that doesn't mean I don't still want to see it. 在许多情况下,该变量实际上从未被任何“做东西”的代码引用,但这并不意味着我仍然不想看到它。 Unfortunately, the optimizer is working against me and simply removing all such code when compiling, so I have to come up with convoluted grossness to fool the compiler into thinking those values actually matter so they don't get optimized away. 不幸的是,优化器正在反对我,只是在编译时删除所有这些代码,所以我不得不提出令人费解的粗俗来欺骗编译器认为这些值实际上很重要,所以它们不会被优化掉。 I don't want to turn the optimizer off, as it's doing important stuff in other places, but just for this one block of code I'd like to temporarily disable it for the sake of debugging. 我不想关闭优化器,因为它在其他地方做了重要的事情,但只是为了这个代码块我想暂时禁用它以便调试。

Code the produces observable behavior fits the requirements by definition. 生成可观察行为的代码符合定义的要求。 For example, printf("") . 例如, printf("")

Access to volatile variable also formally constitutes observable behavior, although I won't be surprised if some compilers still discarded "unnecessary" volatile variables. 对volatile变量的访问也正式构成了可观察的行为,尽管如果一些编译器仍然丢弃“不必要的”volatile变量,我也不会感到惊讶。

For this reason, a call to an I/O function appears to be the best canditate to me. 出于这个原因,对I / O功能的调用对我来说似乎是最好的。

You can try "volatile" keyword. 您可以尝试“volatile”关键字。 Some intro is at http://en.wikipedia.org/wiki/Volatile_variable . 一些介绍在http://en.wikipedia.org/wiki/Volatile_variable

Generally speaking, the volatile keyword is intended to prevent the compiler from applying any optimizations on the code that assume values of variables cannot change "on their own." 一般来说,volatile关键字旨在防止编译器对代码应用任何优化,这些优化假设变量值不能“单独”更改。

Have you tried 你有没有尝试过

#pragma optimize("",off)

?

MSVS specific I think - http://msdn.microsoft.com/en-us/library/chh3fb0k(v=vs.80).aspx 我认为MSVS具体 - http://msdn.microsoft.com/en-us/library/chh3fb0k(v=vs.80).aspx

You do not specify your compiler so I will just add here that I have been using the volatile specifier on any variables that I use specifically for debugging. 你没有指定你的编译器,所以我只是在这里添加我在我专门用于调试的任何变量上使用volatile说明符。 In my compiler (Embarcadero RAD Studio C++ Builder) I have been using this for a couple of years and not once has the variable been optimized out. 在我的编译器(Embarcadero RAD Studio C ++ Builder)中,我已经使用了几年,并没有将变量优化出来。 Maybe you don't use this compiler, but if you do I can say volatile has certainly always worked for me. 也许你不使用这个编译器,但如果你这样做,我可以说volatile一定对我有用。

Here's an example of a trick I've used: 这是我用过的一个技巧的例子:

#include <ctime>
#include <iostream>
int main() {
    std::cout << "before\n";
    if (std::time(NULL) == 0) {
        std::cout << "This should not appear\n";
    }
    std::cout << "after\n";
}

The time() call should always return a positive value, but the compiler has no way of knowing that. time()调用应始终返回正值,但编译器无法知道这一点。

If the only purpose of the variable is to be viewed while in a breakpoint with a debugger, you can make the variable global. 如果在使用调试器的断点中查看变量的唯一目的,则可以将变量设置为global。 You could, for instance, maintain a global buffer: 例如,您可以维护一个全局缓冲区:

#ifdef DEBUG
char dbg_buffer[512];
template <typename T>
void poke_dbg (const T &t) {
    memcpy(dbg_buffer, &t, sizeof(T));
}
#else
#define poke_dbg(x)
#endif

Then during debugging, you can inspect the contents of the dbg_buffer (with an appropriate cast if desired). 然后在调试期间,您可以检查dbg_buffer的内容(如果需要,可以使用适当的强制转换)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM