C C++内存泄漏的原因有哪些 (除了通常分配内存而忘记释放它)
最佳答案
如果在分配和释放之间引发异常,则会发生内存泄漏。
void f1() {
int* ptr = new int;
// do something which may throw an exception
// we never get here if an exception is thrown
delete ptr;
}
每次 f1 异常终止时,都会泄漏 4 个字节(假设 int 是 4 个字节)。
关于c++ - C C++内存泄漏的原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4447391/