Disadvantages of Pointers in C++

Disadvantages of using Pointers in C++ with Examples:

In this article, I am going to discuss the Disadvantages of using Pointers in C++ Language with examples. Please read our previous articles, where we discussed Pointer Arithmetic in C++ with examples.

Disadvantages of using Pointers in C++:

At the end of this article, you will understand what are the problems of using pointers. Pointers are very dangerous when they are not used properly and due to pointers Systems may crash means our software or our program may crash due to run time error. At compile time we may not get the error but at run time we get the error.

Run time errors are very dangerous, the reason is when we deliver our software to a client so when the user is using our program, then the user may face the problem during run time that program may crash. It gives a bad name to a programmer.

Getting an error at runtime is just like if a company is selling a car and the people are complaining about the problems in the car. So, the car model will be failed and they have to take away that model from the market. So, the same thing happened with the software. If our software is giving trouble to the user then the user will avoid using it. So run time errors are very dangerous. We have to be careful with those runtime errors. And one of the major reasons for which we get the runtime errors is pointers.

Now let us see what could be the possible problems using pointers. Following are the major problems with pointers:

  1. Uninitialized Pointers
  2. The pointer may cause a memory leak
  3. Dangling Pointers

These are the three commonly known problems. Mostly these problems are caused due to the negligence of a programmer or carelessness of a programmer. Let us see all the problems with a detailed explanation.

Uninitialized pointers in C++:

int *p;

if we have declared a pointer then we should not use that pointer unless we have initialized it.

*p = 25;

This means that we want to store the value ‘25’ wherever the pointer is pointing. But the question here is where the pointer is pointing? In, ‘int *p’ it is not pointing anywhere. Just we have declared. Then what is the address in ‘p’ here?

Some default garbage addresses may be in ‘p’, some random addresses that may belong to a program or may not belong to a problem. So, it is an invalid address as we have not made a pointer to point to some particular location. So first of all, make it point on some location then we can access it. Then how we can do that? There are 3 methods of doing it.

1st Method:

If we have some variable ‘x’ then,

int x = 10;

int *p = &x;

Now the pointer is pointing to this known variable ‘x’ which is already declared.

2nd Method:

int *p = (int*) 0x5628;

We can assign some addresses using some hexadecimal codes but that address has to be typecasted as an integer pointer. So, can we directly assign some addresses to a pointer? Yes, if we are sure that the address belongs to the program so this type of initialization is also allowed. If required we can use this. This is not commonly used. This is mostly used in systems programming

3rd Method:

int *p = new int[5];

We can dynamically allocate some memory and assign that to a pointer. If we don’t write size and write only ‘int’ then it will allocate just one integer so either to an existing variable. After these methods, we can store the value or read the value in the program we will not get run time error, we will get the data if the data is available. Now the second problem is a memory leak.

Memory Leak:

This is related to a pointer as well as heap memory. As we have already discussed heap memory that when we are allocating heap memory then when we don’t require that, we should de-allocate it. If we don’t de-allocate it then we say that memory is leaked from that total set of memory.

int *p = new int[3];

….

p = NULL;

Here we have a pointer and I have allocated heap memory of some size. Then after some time, we don’t need this memory. So, we simply say ‘p = NULL’, then point P will not be pointing on that memory.

Suppose we have an array on which a pointer is assigned and now we have removed this pointer from there, null means we have removed the pointer from there. We should not do this unless we have explicitly deleted the memory. So first of all, say delete ‘p’ then only, make ‘p’ as null.

delete []p;

p = NULL;

Now here is one more thing that we can write ‘p = 0’ also or write ‘p = nullptr‘.

In modern C++ it is suggested to use ‘nullptr’. You should avoid using null. So back to this memory leak, the conclusion is you must delete the memory when you are not using it before making a pointer to null. Now let us move to the third problem which is the dangling pointer.

Dangling Pointer in C++:

We will show you one situation where this type of problem may arise.

void main(){
    int *p = new int[5];
    ….
    fun(p);
}
void fun(int *q){
    …
    …
    delete []q;
}

Here we have a main function ‘void main’, inside this we have a pointer and to this, we have allocated heap memory as ‘new int[5]’. Now we have called one function ‘fun’ and we have sent a pointer ‘p’. Now the function ‘fun’ which is taking a pointer as a parameter is using the memory then after that it is saying ‘delete []q’.

So, the memory where ‘q’ was pointing will be deleted at the end of the function ‘fun’. So, function ‘fun’ deleted the memory using pointer ‘q’ which was shared by the main function. Now when the control comes back to main, inside the main function if you tried to access, access means if we want to print some value,

cout << *p;

Now, ‘p’ is pointing to a location that does not exist the memory is deallocated. So actually, this will cause an error, run time error, the pointer is trying to access the memory which is no more belonging to a program which is deallocated now. This type of problem may also cause when you are trying to access the memory which is already deallocated. Here the pointer ‘p’ is now dangling pointer.

So uninitialized pointers mean, the pointer is never initialized, dangling pointer means the pointer was initialized but memory is deallocated. We should avoid these three types of problems while writing programs or developing applications. Actually, these problems are caused due to negligence of beginner programmers. Expert programmers may check all these things thoroughly only before delivering a program or before developing software.

Beginner programmers may do these types of mistakes. So, to make it simple for the programmer to avoid these types of dangerous errors, java, and Dot Net has removed pointers and those languages are managed languages so JVM will take care of all these things it will try to de-allocate the memory when not in use and it will not allow dangling pointers to access so they take care in their own way.

So that’s why those languages have been called manage languages, and they are much simpler than C++ but C++ gives full control to the programmer. This is more powerful but the programmer must be careful while using pointers.

What are the problems we usually face while using pointers?

Problem1: We already know we need to explicitly deallocate a memory but what if we forgot to deallocate. Yes, if we forgot to deallocate a memory then it will lead to a memory leak. Let’s see how?

Disadvantages of Pointers in C++ Language with examples

Continues Memory leak will cause the program to crash

Problem no2: Pointer Un-initialization results in a runtime error.
#include<iostream>
using namespace std;
int main() {
   int *ptr;
   cout<<*ptr;
   return 0;
}
Output:

Pointer Un-initialization results in a runtime error

Avoid using the uninitialized pointer.

Problem no3: Dangling pointer

Let’s explain the concept. If two pointers point to the same memory location and pointer 1 deallocates the memory but pointer 2 trying to access the memory thinking it is available is called dangling pointer.

Problem no3: Dangling pointer

Program to illustrate Dangling Pointer in C++:
#include<iostream>
using namespace std;
int main() {
   int *ptr1=new int(10);
   int *ptr2=ptr1;
   delete []ptr2;
   cout<<*ptr1;
   return 0;
}

Here ptr1 is a dangling pointer.

Output:

Disadvantages of Pointers in C++ Language with examples

In the next article, I am going to discuss References in C++ with examples. Here, in this article, I try to explain the Disadvantages of Pointers in C++ Language with examples. I hope you enjoy this Disadvantages of Pointers in C++ with examples article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

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