开发者

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:

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? Generally, what kind of heap errors are detected by gflags, and what errors are not detected?


But this program doesn't crash:

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

Why gflags doesn't catch this?

Because the new operation will often allocate memory more than you want, for memory alignment purpose. If you want to crash this, just use p[1025] = 0; or something larger.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜