Working with pointers in C ++: dangers and potential pitfalls

One of the most powerful tools provided in C ++ for a programmer is pointers. Pointers in C ++ are used to directly access different memory locations that are allocated while compiling and running a program. This provides an enormous amount of flexibility, depth, and strength to the program being written and adds another dimension to the object-oriented paradigm.

However, as we all know from the superhero comics of our childhood: with great power comes great responsibility (God, that sounded so cliché). Therefore, pointers in C ++ they are a double edged sword. Some of these pointer problems or specific dangers they are memory leaks or inaccessible memory and hanging pointers.

This article covers some of the main dangers of pointers and the steps any C ++ programmer should take to avoid them.

Specific dangers

The main dangers of the indicators are:

1. Memory leak
2. Hanging pointer
3. Wrong pointer

Memory loss:
Memory leaks are a common occurrence in many C ++ code. Basically this happens when a pointer, already pointing to a memory block, say ‘A’, is reassigned to another memory block, say, ‘B’. Since the only way to directly access allocated memory blocks is through pointers, the first location, A, now becomes completely inaccessible memory, causing a memory leak. Since the compiler does not deallocate memory by itself (the programmer has to use the delete keyword) and since our pointer now points to memory location B, this wastes memory and resources on the system being used.

Example:
int * ptr = nullptr;
ptr = new int;
* ptr = 5;
ptr = new int;

Here the memory space that stores the integer ‘5’ is now inaccessible as the ‘ptr’ pointer now points to another memory location. As a result, the space the system uses to store an integer is wasted until the end of program execution.

As a general rule of thumb to avoid memory leaks, always make sure the number of ‘remove’ and ‘new’ keywords in your program is equal, as every time you allocate new memory using ‘new’ you have to deallocate it using ‘remove ‘.

Hanging pointer:
A hanging pointer is a pointer that points to a location in memory that is not part of your program. Occurs when a programmer does not point a pointer to another memory location after using the ‘delete’ instruction.

Example:
int * ptr = new int;
* ptr = 10;
remove ptr;
cost << * ptr;

Notice here the integer memory that was allocated to the pointer has been removed, but we are still trying to un-reference the pointer and generate its value. It can lead to very serious situations in your program that cause an infinite loop or a program hang.

Bad pointer:
Always initialize a pointer when creating it, otherwise it would become a bad pointer and point to any arbitrary location in memory. Leaving this pointer uninitialized could cause system crashes. Since an uninitialized pointer points to a random memory location, this location could very well be a memory location previously used by another pointer, causing significant problems in the program.

Example:
int * ptr;
cost << * ptr;

Normally you have to initialize it with a NULL value first and some other value later in your program. The new standard has created the keyword ‘nullptr’ for this purpose.

Example:
int * ptr = nullptr;

It is easy to see that pointers in C ++ must be handled with extreme care and caution. There are many possibilities that can lead to crashes and errors in your programs while using pointers, but if you try to avoid the dangers of pointers mentioned above, in general you should be fine !!!

Leave a Reply

Your email address will not be published. Required fields are marked *