【发布时间】:2010-11-15 12:31:25
【问题描述】:
如果我们在 char 数组的 new 和 delete 之间有一大段代码,为什么会出现问题。
void this_is_bad() /* You wouldn't believe how often this kind of code can be found */
{
char *p = new char[5]; /* spend some cycles in the memory manager */
/* do some stuff with p */
delete[] p; /* spend some more cycles, and create an opportunity for a leak */
}
【问题讨论】:
-
我不确定您真正要问的是什么。在 "new" 和 "delete" 之间有很多代码并没有错。不管你有多少代码,堆损坏都是不好的。
-
问题似乎是“给定的代码如何为泄漏创造机会?”
-
我敢打赌这是一个很快就要到期的家庭作业问题。
标签: c++ heap-memory heap-corruption