在Visual Studio中調試C++項目時,某些數據斷點從未命中。爲什麼數據斷點無法在未對齊的地址上工作
所以我寫了一些代碼來進行測試:
#include <iostream>
#include <stdint.h>
void test(uint32_t* p)
{
*p = 0;
// set a data breakpoint on p
*((char*)p + 2) = 0x1;
std::cout << *p << std::endl;
}
uint32_t* alloc(size_t offset)
{
char* p = new char[sizeof(uint32_t) + offset];
p = p + offset;
return (uint32_t*)p;
}
int main()
{
test(alloc(0)); // test #1
test(alloc(2)); // test #2
}
正如你看到的,在性能測試中,* p的值將首先被清零, 那麼它將被隱式改變,我得到了一個litte- endian CPU,所以它 必須是65536.
如果你設置一個數據斷點在p(4字節)檢測變化, 你會得到兩個不同的結果:命中與否。這取決於該p指向的地址 。
在上述我的測試代碼,測試#1將擊中和試驗#2將不會, 之間#1和#2的差別是由 的alloc返回的地址(0)或Alloc(2)。
這篇文章How to: Set a Data Breakpoint在MSDN上不討論這個。
數據斷點不能在未對齊的地址上工作嗎?