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;}

Sample Code:

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

See also  How to Create and Deploy QR Codes Online: A Comprehensive Guide

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist