Visual Studio Breakpoint Macro to modify a value?

后端 未结 5 1204
一个人的身影
一个人的身影 2021-01-05 12:25

I\'m debugging an application (C++), and I\'ve found a point in the code where I want to change a value (via the debugger). So right now, I\'ve got a breakpoint set, whereu

5条回答
  •  青春惊慌失措
    2021-01-05 13:09

    If you are think of a macro in the same way as Microsoft excel, then you're out of luck. It doesn't quite work that way.

    In C++, a macro refers to a small inline function created with #define. It is a preprocessor, so a macro is like using a replace on all its references with its body.

    For example:

    #define add(a,b) ((a)+(b))
    
    int main() {
      int a=3, b=4, c=5, d=6, e, f;
      d = add(a,b);
      e = add(c,d);
    }
    

    Would like to the c++ compiler as:

    int main() {
      int a=3, b=4, c=5, ...;
      d = ((a)+(b));
      e = ((c)+(d));
    }
    

    Now, back to your question. If the variable is within scope at this breakpoint, just set it from within your code:

    myVar = myValue;
    

    If it is not, but it is guaranteed to exist, you may need a little hack. Say that this variable is an int, make a global int pointer. If this variable is static, make sure to set it to its address, and back to NULL inside it's scope. If it is dynamic, you may need some extra work. Here is an example:

    int* globalIntPointer;
    
    void func() {
      *globalIntPointer = 3;
      //...
    }
    
    int main() {
      int a = 5;
      globalIntPointer = &a;
      func();
      //...
      globalIntPointer = NULL; // for safety sake
      return 0;
    }
    

提交回复
热议问题