CRT detected that the application wrote to memory after after the end of heap buffer(这个经常忘掉)

在VS2005写完程序后,运行后弹出对话框显示:

HEAP CORRUPTION DETECTED:

CRT detected that the application wrote to memory after after the end of heap buffer

最后定位到代码

ans = new int[size_a+size_b];        // 动态分配数组空间

delete [] ans;

一把delete [] ans;删掉程序就不弹出错误,这是为什么呢??

错误原因

因为对内存的操作越界了,超出所分配的内存的边界。

解决:

增大分配的内存!   

e.g.

ans = new int[size_a+size_b +1 ];     

或者加到自己适宜的大小,问题即可解决...

总结:

对内存的操作要细之又细,new完后要delete,操作时不要越界(包括向前越或向后越).......

.....


原文地址:https://www.cnblogs.com/lzjsky/p/1964098.html