每當我輸出特定的指針地址std::cout
,我得到一個崩潰:什麼可能導致寫指針地址到std :: cout崩潰?
bool MyClass::foo() const
{
std::cout << "this prints fine" << std::endl << std::flush;
std::cout << d << std::endl << std::flush; // crash!
return true;
}
哪裏d
是類的指針成員,即:
class MyClass {
// ...
private:
MyClassPrivate* d;
};
什麼會導致應用程序崩潰?即使它是一個NULL指針或初始化指針,它仍然應該打印出(也許是無效的)地址,對吧?
應用程序在調試模式下編譯,如果這有所幫助。功能foo
未標記爲內聯。
背景:我試圖追蹤外部應用程序進程中的錯誤。該錯誤僅在另一個應用程序向該進程發送快速命令時引起。我使用std::cout
來追蹤外部進程的執行情況。
也許你有operator <<(ostream&,MyClassPrivate *)定義?這將導致它被稱爲 – killogre
你可以告訴崩潰是在'operator <<'還是在'foo()'中? – zneak
*旁白*:'std :: endl'已經執行了'std :: flush'。代碼中的std :: flush是多餘的。 –