你在C/C++的土地。有一點你不能做。
如果這是您自己的代碼,您甚至不應該嘗試這樣做。像其他人一樣建議:通過引用(或通過C中的指針)傳遞一個輸出參數,或者返回一個結構體中的值。
不過,既然你問的問題,我假設你正試圖尋找到的東西,你只需要二進制訪問。如果只是一次性的話,使用調試器會更容易。
總之,要回答你原來的問題,請嘗試以下代碼。您必須爲x86 CPU編譯它,並關閉優化和任何堆棧調試標誌。
void f() {
int i = 12345;
int j = 54321;
}
int main()
{
int* pa = 0;
int buf[16] = {0};
f();
// get the stack pointer
__asm {
mov dword ptr [pa],ESP
}
// copy the stack, try not to do anything that "use" the stack
// before here
for (int i = 0; i < 16; ++i, --pa) {
buf[i] = *pa;
}
// print out the stack, assuming what you want to see
// are aligned at sizeof(int)
for (int i = 0; i < 16; ++i) {
std::cout << i << ":" << buf[i] << std::endl;
}
return 0;
}
在C/C++的土地,有很多你不能這樣做,包括這個。你的意思是,「你在本地代碼的土地,並且你可以用實施細節搞砸,所以你沒有什麼不能做的」) – jalf 2009-07-10 15:31:06