问题
I use new to allocate a buffer, as follows:
BYTE *p;
p = new BYTE[20];
...
delete p;
After p
is deleted, if I do NOT assign NULL to p
, is there a way to determine whether it has already been freed?
回答1:
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.
来源:https://stackoverflow.com/questions/27374640/how-to-determine-if-a-buffer-is-freed-or-not