devxlogo

Make Sure You Don’t Leave Pointers to Deleted Things Hanging Around

Make Sure You Don’t Leave Pointers to Deleted Things Hanging Around

The following macros will facilitate this task and have been very useful:

 #define SAFEDELETE(p) if(p) { delete p; p =NULL;}#define SAFEDELETE_ARRAY if(p) { delete [] p; p = NULL;}How to use-int main(){    int *i  = new int;    char *ptr = new char[100];    // do something with ptr    SAFEDELETE_ARRAY(ptr);    SAFEDELETE(i);}

size=3>

devx-admin

Share the Post: