Working in C++ is considered relatively difficult because the programmer is faced with many additional headaches; biggest of them being the memory management and the consequent pointer hazards. The allocation and de-allocation of memory at the appropriate time is not an easy job. But this very feature makes C++ one of the fastest and efficient Programming Languages. Pointers are used to access the memory blocks in C++. I assume that you are already familiar with the pointers. So I shall explain some of the dangers you might be in while working with pointers. Then gradually we shall advance towards more advanced usage and hazards.
The familiar syntax of allocation of an address to a pointer in C++ is as follows.
</p> <p>int num = 10;</p> <p>int *ptr = & num;</p> <p>int *ptr = new int ; //And the dynamic allocation is done through</p> <p>
Some of the important topics with regard to Pointer Hazards are as follows.
- Dangling vs. Bad Pointers
- Null vs. Void Pointer
- Orphaned Memory vs. Memory Leak
Let us see them in more detail.
1. Null vs. Void Pointer
As soon as a pointer is given birth, it should always be assigned a zero or NULL value. In Microsoft Visual Studio, it is done like this.
</p> <p>int *ptr = nullptr ;</p> <p>
It makes this pointer perfectly safe to use and is known as a null pointer. It does not point to any memory location. Deleting a null pointer is perfectly safe as well.
Now you know very well that a pointer can point to any one particular data type. So at the time of creating a pointer, it is compulsory to mention the data type with the pointer and only the address of that type of variable can be stored in it. A pointer which is not bound to any specific data type is known as a void pointer. During runtime, it can point to data type of your choice. Have a look at the code below.
</p> <p>void *ptr = nullptr ;</p> <p>ptr = new char ;</p> <p>ptr = new int ;</p> <p>
Remember that you cannot dereference the void pointer without allocating it any memory. Think why?
2. Dangling buy viagra online vs. Bad Pointers
When you simply declare a pointer and not assign it any address or value, it may be pointing towards any arbitrary location in the memory. Such a pointer is known as a bad pointer. For all practical purposes, a bad pointer can become quite dangerous during program execution. So it is advisable to always initialize the pointer to nullptr.
Now you are working with pointers in your program. You have written the following piece of code.
</p> <p>int *ptr = new int ;</p> <p>delete ptr ;</p> <p>
First you allocated the memory to the pointer and then deleted it. Your program no longer owns that memory. But notice that the pointer is still pointing towards that memory which has been deleted. This pointer is now known as a dangling pointer. Dereferencing this pointer now may lead to serious consequences and the program may crash at a later stage. To avoid a dangling pointer, you should make the pointer nullptr after deletion of its memory.
3. Orphaned Memory vs. Memory Leak
Playing with pointers, it is entirely the responsibility of programmer to de-allocate the memory that was allocated at any time in the program using new. In case of not doing so, serious problems do arise. Look at the following piece of code.
</p> <p>void *ptr = nullptr;</p> <p>ptr = new int ;</p> <p>ptr = new char ;</p> <p>
In this masterpiece that has been produced, I first allocated an integer memory to the pointer and then the character memory. But did you observe that the integer memory was dynamically allocated and it was not deleted before assigning the character memory to the pointer. So the integer memory has become inaccessible now and is known as orphaned memory. This type of situation is known as memory leak.
Memory leak is one of the most dangerous and commonly occurring programming causes for crashing. If the program is quite large and memory leak is occurring, system memory may become in sufficient. The best trick to avoid it is to keep track of how many times you have used new operator in your program. You should use delete as many times in your program when you are finished working with that particular memory.
From now onwards, I would start with the Object Oriented Paradigm and Operator Overloading apart from our routine posts on Sorting Algorithms. Good Bye!