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?
您无法确定这一点,这是您通常依赖于高级机制来处理内存的主要原因之一,例如智能指针( shared_ptr
, unique_ptr
等),或者在您的情况下使用std::vector
进行处理原始内存,以确保不会因未定义行为启动程序的两次delete
,忘记delete
或其他令人讨厌的错误。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.