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 a program is being compiled and executed. 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 our childhood superhero comics: With great power comes great responsibility (God that sounded so cliché). Thus, pointers in C ++ are a double-edged sword. A few of such pointer problems or pointer hazards are memory leak or inaccessible memory and dangling pointers.
This article covers some main pointer hazards and the steps that should be taken by any C ++ programmer to avoid such pointer hazards.
Pointer Hazards
The main pointers hazards are:
1. Memory Leak
2. Dangling Pointer
3. Bad pointer
Memory Leak:
Memory leaks are a common occurrence in many C ++ codes. Basically, this occurs when a pointer, already pointing at a memory block, let’s say ‘A’, is reallocated to another memory block, say, ‘B’. As the only way to access allocated memory blocks directly is through pointers, the first location, A, now becomes completely inaccessible memory, leading to a memory leak. As the compiler does not de-allocate memory by itself (the programmer has to use the delete keyword) and as our pointer is now pointing towards memory location B, this causes wastage of memory and resources of 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’ now cannot be accessed as the pointer ‘ptr’ is pointing towards another memory location now. As a result the space that the system uses for storing one integer is wasted until the end of program execution.
As a rule of thumb to avoid memory leak, always make sure the number of ‘delete’ and ‘new’ keywords in your program are equal, as every time you allocate a new memory using ‘new’, you need to de-allocate it using ‘delete’.
Dangling Pointer:
A dangling pointer is a pointer which points to a location in memory that is not a part of your program. It occurs when a programmer fails to point a pointer towards another memory location after using the ‘delete’ statement.
Example:
int * ptr = new int;
* ptr = 10;
delete ptr;
cout << * ptr;
Notice that here, the integer memory that was allocated to the pointer has been deleted but we are still trying to dereference the pointer and output its value. It may lead to very serious situations in your program leading to an infinite loop or a program crash.
Bad pointer:
Always initialize a pointer when creating it, otherwise it would become a bad pointer and point to any arbitrary location in the memory. Leaving this pointer uninitialized could lead to system crashes. As an uninitialized pointer points to a random memory location, this location could very well be a memory location previously used by another pointer, leading to major problems in the program.
Example:
int * ptr;
cout << * ptr;
Normally you should first initialize it to a NULL value 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 ++ should be handled with extreme care and caution. There are many possibilities that may lead to crashes and bugs in your programs while using pointers, but if you try to avoid the pointers hazards mentioned above, you should be generally fine !!!
Source by Raza Ahmad