如何确定是否释放了缓冲区 [英] How to determine if a buffer is freed or not
本文介绍了如何确定是否释放了缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用new来分配缓冲区,如下所示:
I use new to allocate a buffer, as follows:
BYTE *p;
p = new BYTE[20];
...
delete p;
删除p
后,如果我不为p
分配NULL,是否有办法确定它是否已被释放?
After p
is deleted, if I do NOT assign NULL to p
, is there a way to determine whether it has already been freed?
推荐答案
您无法确定,这是您通常依赖高级机制来处理内存的主要原因之一,例如智能指针( shared_ptr
,未定义行为登陆的程序中令人讨厌的错误.
You cannot determine that, and that's one of the main reasons you usually rely on higher-level mechanisms to handle memory, such as smart pointers (shared_ptr
, unique_ptr
and so on), or in your case rather std::vector
, to deal with raw memory in a way that guarantee no double delete
, forgotten delete
, or other nasty mistakes that launch your program in undefined behavior land.
这篇关于如何确定是否释放了缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文