devxlogo

Handling Pointers Safely

Handling Pointers Safely

There are various problems regarding pointers that can be handled with the help of macros. Some of these problems are: deleting dangling pointers, deleting pointers other than NULL, and checking for NULL before deleting pointers.
The following macros will take care of these issues:

 #define POINTERDELETE(m) if(m) {delete m;m =NULL;}#define POINTERARRAYDELETE if(m) { delete [] m; m = NULL;}

size=3>
Sample Code:

 int main(){    int *p  = new int;    char *ptr = new char[100];    // different operations with pointers    POINTERARRAYDELETE(ptr);    POINTERDELETE (p);

size=3>

devx-admin

Share the Post: