T
thomas
foo *p = new foo();
----------
1) if(p!=NULL) delete p;
2) delete p; p = NULL;
3) delete p; delete p;
----------------------code-------------------
Double free can be a disaster in the case of 3).
To avoid "double free", it's better to set the pointer NULL when
released.
Since C++ guarantees "NULL pointer" is not handled, 1) is not
necessary.
My question is whether C++ gurantees the pointer is reset to "NULL"
after the memory is freed?
Or whether the expression "p=NULL;" in case 2) necessary?
----------
1) if(p!=NULL) delete p;
2) delete p; p = NULL;
3) delete p; delete p;
----------------------code-------------------
Double free can be a disaster in the case of 3).
To avoid "double free", it's better to set the pointer NULL when
released.
Since C++ guarantees "NULL pointer" is not handled, 1) is not
necessary.
My question is whether C++ gurantees the pointer is reset to "NULL"
after the memory is freed?
Or whether the expression "p=NULL;" in case 2) necessary?