VS中遇到断点继续执行时,如何在运行时自动修改变量值?

How to modify value of variable automatically at runtime when hitting a breakpoint and continuing execution in VS?

我们可以通过更改变量工具提示或local/auto/watch window 手动更改值。 但我想自动将变量的值更改为某个特定的硬编码值或基于代码片段。 例如-

int main()
{
int a=0,b=1,c=2;
//bla bla
for(int i=0; i<100; ++i)
{
executeMe();
}
//bla bla
}

我想在行 "executeMe()" 上放置断点并将 'b' 的值更改为硬编码值 3 或基于变量值 'c',因此执行指令 'b=c' .并继续执行而不会每次都在断点处停止。 如何在 VS 中执行此操作?

您可以使用#if 预处理器指令,这与以下代码类似。

        int a = 0, b = 1, c = 2;
        for (int i = 0; i < 100; ++i)
        {
#if DEBUG
            b=3;
#endif
            executeMe();
        }

使用 'Print a message:' 选项而不是宏。来自代码的值可以通过将它们放在 {} 中来打印。关键是 VS 也会将内容评估为表达式 - 所以 {variable_name=0} 应该实现与宏示例相同的效果。

感谢 Tom McKeown 在 Whosebug 上提供此解决方案。com/a/15415763/2328412