Visual Studio断点被移动

时间:2012-02-21 20:05:11

标签: c++ visual-studio debugging visual-c++ breakpoints

我最初使用的是Visual Studio C ++ Express,我已经切换到终极版,目前我很困惑调试器为什么要移动我的断点,例如:

if(x > y) {
    int z = x/y;         < --- breakpoint set here
}
int h = x+y;             < --- breakpoint is moved here during run time

random line of code      < --- breakpoint set here
random line of code

return someValue;        < --- breakpoint is moved here during run time

似乎在代码中的随机位置执行此操作。有时候我在这里做错了吗?我从未遇到像这样的快递版本的问题。

1 个答案:

答案 0 :(得分:10)

您正在以发布模式进行调试。

if(x > y) {
    //this statement does nothing
    //z is a local variable that's never used
    //no executable code is generated for this line
    int z = x/y;         < --- breakpoint set here
}
//the breakpoint is set on the next executable line
//which happens to be this one
int h = x+y;             < --- breakpoint is moved here during run time

通常调试器在二进制代码中设置挂钩。如果没有为int z = x/y执行二进制代码,则无法在那里设置断点。

通过在发布模式下编译以下内容生成以下内容:

if(x > y) 
{
    int z = x/y;//         < --- breakpoint set here
}
int h = x+y;
cout << h;
003B1000  mov         ecx,dword ptr [__imp_std::cout (3B203Ch)] 
003B1006  push        7    
003B1008  call        dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (3B2038h)]

要对此进行测试,您可以执行以下简单的更改:

if(x > y) {
    int z = x/y;
    std::cout << z << endl; // <-- set breakpoint here, this should work
}
int h = x+y;