我嘗試使用windbg在進入成員函數時打印「this」指針,如下所示。爲什麼VS和Windbg即使在調試版本中也將「this」指針打印爲「0xcccccccc」?
class IBase {
int m_i;
public:
IBase() :m_i(23) {}
virtual int FuncOne(void) = 0;
virtual int FuncTwo(void) = 0;
};
class DerivedOne : public IBase {
public:
virtual int FuncOne(void) { return 1; };//set break point here.
virtual int FuncTwo(void) { return 2; };
};
class DerivedTwo : public IBase {
public:
virtual int FuncOne(void) { return 101; };
virtual int FuncTwo(void) { return 102; };
};
void DoIt(IBase* Base)
{
int i=Base->FuncOne();//break point here
}
int main(int argc, char *argv[])
{
DerivedOne d1;
DerivedTwo d2;
DoIt(&d1);
DoIt(&d2);
return 0;
}
(1)我與VC2015調試版本(32位)編譯它
(2)我設置 「大一」 功能的破發點。 (3)當點擊Base-> FuncOne()時,我按下「F11」進入DerivedOne的功能。
現在我可以看到調用堆棧是這樣的:
0:000> k
# ChildEBP RetAddr
00 0041f654 0022157c ConsoleApplication1!DerivedOne::FuncOne [d:\documents\visual studio 2013\projects\consoleapplication1\consoleapplication1.cpp @ 13]
01 0041f734 0022173c ConsoleApplication1!DoIt+0x2c [d:\documents\visual studio 2013\projects\consoleapplication1\consoleapplication1.cpp @ 23]
02 0041f850 00221dc9 ConsoleApplication1!main+0x7c [d:\documents\visual studio 2013\projects\consoleapplication1\consoleapplication1.cpp @ 36]
03 0041f8a0 00221fbd ConsoleApplication1!__tmainCRTStartup+0x199 [f:\dd\vctools\crt\crtw32\dllstuff\crtexe.c @ 626]
04 0041f8a8 75b9338a ConsoleApplication1!mainCRTStartup+0xd [f:\dd\vctools\crt\crtw32\dllstuff\crtexe.c @ 466]
05 0041f8b4 77529902 kernel32!BaseThreadInitThunk+0xe
06 0041f8f4 775298d5 ntdll!__RtlUserThreadStart+0x70
07 0041f90c 00000000 ntdll!_RtlUserThreadStart+0x1b
但是, 「DV」 命令給了意想不到的結果
0:000> dv
this = 0xcccccccc
這是爲什麼?該程序運行良好,調試版本不優化任何東西,似乎一切都很好。但爲什麼「這個」指針是無效的?
我用VC自己的IDE來調試,同樣的觀察。但爲什麼?
'這個'沒有在'Doit'函數中定義:它不是您的任何類的成員。 –
如果您將'return 1'放在另一行上,您將在那裏看到一個有效的'this'。 – rustyx
就像RustyX說的那樣,除非函數調用堆棧,否則不能進入單行函數。至少不適用於Visual Studio。將聲明放在單獨的行上。 – UmNyobe