2012-02-21 147 views
6

我最初使用Visual Studio的C++ Express中,我已經切換到最終和IM目前困惑,爲什麼調試器是移動我的斷點,例如:Visual Studio中斷點移動

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 

它似乎在代碼中的隨機位置執行此操作。有時候我在這裏做錯了嗎?我從來沒有像這樣的快遞版本發生問題。

回答

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;    
+0

我同意這是最likly原因,我會還要注意,在過去,由於線路終端差異(NL vs CR NL),我看到通過調試器和IDE報告的線路之間存在不一致(disconects)。我記得Borland Delphi產品中存在一個大問題,但我不認爲這是是VS的問題。 – tletnes 2012-02-21 20:20:11

+0

@tletnes嗯有趣,我從來沒有遇到過這個雖然在VS. – 2012-02-21 20:23:01

+0

對!我完全忽略了我處於發佈模式。謝謝! – kbirk 2012-02-21 21:37:31