简体   繁体   English

为什么Windows的gflags不会因该代码而崩溃?

[英]Why doesn't Windows' gflags fail to crash with this code?

I made the following program: 我做了以下程序:

int main() {
    int* p = new int[10];
    delete[] p;
    p[0] = 0;
    return 0;
}

Then I executed this program with gflags enabled: 然后我在启用gflags的情况下执行了该程序:

C:\tmp\Test2\Debug>"C:\Program Files\Debugging Tools for Windows\gflags.exe" -p /enable Test2.exe /full
path: SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options
    test2.exe: page heap enabled

C:\tmp\Test2\Debug>test2

C:\tmp\Test2\Debug>

As expected, the program crashes. 如预期的那样,程序崩溃。 Running it with a debugger I can see that it crashes at this line: 使用调试器运行它,我可以看到它在此行崩溃:

p[0] = 0;

That's what I expected. 这就是我的期望。

However, this program doesn't crash: 但是,此程序不会崩溃:

int main() {
    int* p = new int[10];
    p[10] = 0;
    return 0;
}

Why doesn't gflags catch this out-of-bounds access? 为什么gflag无法捕获这种越界访问? Generally, what kind of heap errors are detected by gflags, and what errors are not detected? 通常,gflags检测到哪种堆错误,而未检测到哪些错误?

But this program doesn't crash: 但是该程序不会崩溃:

int main() {
    int* p = new int[10];
    p[10] = 0;
    return 0; 
}

Why gflags doesn't catch this? 为什么gflags不能抓住这个?

Because the new operation will often allocate memory more than you want, for memory alignment purpose. 因为new操作经常会分配过多的内存,以达到内存对齐的目的。 If you want to crash this, just use p[1025] = 0; 如果要崩溃,只需使用p[1025] = 0; or something larger. 或更大的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM