How to determine if a buffer is freed or not

别来无恙 提交于 2020-01-05 07:46:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!