Visual Studio断点被移动

kbi*_*irk 7 c++ debugging breakpoints visual-studio visual-c++

我最初使用的是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
Run Code Online (Sandbox Code Playgroud)

要么

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

return someValue;        < --- breakpoint is moved here during run time
Run Code Online (Sandbox Code Playgroud)

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

Luc*_*ore 11

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

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
Run Code Online (Sandbox Code Playgroud)

通常调试器在二进制代码中设置挂钩.如果没有执行二进制代码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)]
Run Code Online (Sandbox Code Playgroud)

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

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