C语言 “CRT 检测到应用程序在堆缓冲区结束后写入内存”是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24039299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does "CRT detected that the application wrote to memory after end of heap buffer" mean?
提问by user3699827
I am having trouble with this code. It breaks at the free(q->izv) function and i get a debug error saying:
我在使用此代码时遇到问题。它在 free(q->izv) 函数处中断,我收到一个调试错误说:
CRT detected that the application wrote to memory after end of heap buffer
I have no idea what that means so i would be grateful for any help I get.
我不知道这意味着什么,所以如果我得到任何帮助,我将不胜感激。
typedef struct izvodjaci{
char *izv;
int broj;
struct izvodjaci *sled;
}IZV;
obrisi_i(IZV *p){
while (p){
IZV *q;
q = p;
p = p->sled;
if (!strcmp(q->izv,"UNKNOWN")) free(q->izv);
free(q);
}
}
Thanks in advance
提前致谢
回答by Eric Lippert
What does “CRT detected that the application wrote to memory after end of heap buffer” mean?
“CRT 检测到应用程序在堆缓冲区结束后写入内存”是什么意思?
Suppose you allocate a heap buffer:
假设您分配了一个堆缓冲区:
char* buffer = malloc(5);
OK, buffernow points to five chars on the heap.
好的,buffer现在指向堆上的五个字符。
Suppose you write six chars into that buffer:
假设您将六个字符写入该缓冲区:
buffer[0] = 'a';
buffer[1] = 'b';
buffer[2] = 'c';
buffer[3] = 'd';
buffer[4] = 'e';
buffer[5] = '##代码##';
You have now corrupted the heap; you were only allowed to write five chars and you wrote six.
你现在已经破坏了堆;你只被允许写五个字符,你写了六个。
The program is now permitted to do anything whatsoever. It can work normally, it can crash, it can send all your passwords to hackers in China, anything.
该程序现在可以做任何事情。它可以正常工作,可以崩溃,可以将您所有的密码发送给 CN 的黑客,什么都可以。
Your implementation apparently chooses the best possible choice, which is "inform you that you made a mistake". You should be very, very happythat this is what happened, instead of any of the horrible alternatives. Unfortunately it informs you when the buffer is freed, and not when you made the mistake, but be happy that you got an error at all.
您的实现显然选择了最佳选择,即“通知您您犯了错误”。你应该非常非常高兴这就是发生的事情,而不是任何可怕的选择。不幸的是,它会在缓冲区被释放时通知您,而不是在您犯错时通知您,但很高兴您得到了错误。

