Main problems with pointers in C/C++

Pointers are a powerful tool available when you are programming in C/C++. As usual, we must be careful with the power we have. Let’s see the main problems we can face when working with pointers.

There two main problems programmers face when working with pointers in C/C++: null references and garbage. C and C++ compilers don’t have a garbage collector. Therefore, we have to manage ourselves the memory our program uses.

Keep reading to find out more about these two common problems and how to avoid them.

Pointers in C/C++: A brief introduction

To understand pointers, first, we have to understand what is a variable.

Informally, a variable in programming is a “holder” for data. Any time you need to do some operation with data, you probably will need to use a variable.

Variables

For instance, you want to create a calculator. Calculators work with numbers, so you need to have a mechanism to store the numbers you want to use. that mechanism is called variables. 

Although I gave a simplified explanation, it is one of the most important concepts to learn when learning programming.

Let’s examine the code below.

int a = 5;
int b = 2;

In the previous code, we did two things. First, we declared an int variable (a), and secondly, we assigned the value 5 to the variable a. See below a graphic representation.

graphic representation of variables in memory

Variable a represents a memory block that has the value 5. So, anytime we want to access the value 5, we can do it through the variable a.

Pointers

Pointers in C/C++ are closely related to variables. Usually, we use pointers as another way to access the data stored in memory.

But, can we do that with variables? Why do we need to learn something else to do the same?

The answer here is simple: there are things we cannot do with variables. For those extra things, we are going to use pointers.

The syntax around pointers is easy to understand. We declare a pointer using ‘*’, and in the same way, we get access to the information stored in the memory that the “pointer is pointing to”.

int * intPointer;
pointer1 = (int*) malloc(sizeof(int));
*pointer1 = 5;
cout << intPointer;
cout << *intPointer;
int a = 10;
free(intPointer);
intPointer = &a;
graphic representation of pointers in c/c++

That’s most of the syntax involved in working with pointers.

We use malloc to create a new memory space for the pointer and free to release that memory (previously created with malloc).

Problem with pointers #1: Undefined behaviour

Undefined behaviour can be a common mistake for beginners when working with pointers. The explanation is easy. However, sometimes to avoid them can be more difficult than it appears to be.

Imagine we have two pointers pointing to the same memory address, and we release the shared memory using one of them. What happens to the other one? It will be pointing to an address that was released and can be allocated again if we use malloc.

pointer1 = (int*) malloc(sizeof(int));
*pointer1 = 5;
pointer2 = pointer1;
free(pointer1);

//Don't access pointer1 or pointer1 from here
//unless you assign them new values.

If we try to use the other pointer after that, eventually we are going to have a problem.

The solution to avoid this is very easy. Before freeing the memory allocated to a pointer, we can assign null to the other (s). Then, before we try to use the pointer, we always check if the pointer is not null.

Problem with pointers in #2: Garbage

Garbage is another common problem a worst than the first one. It is the one that can cause (the infamous) memory leaks.

It happens when we only have one pointer pointing to a memory address. If we assign a new value to the pointer without releasing that memory, we will have data in memory that is inaccessible.

graphical example of garbage when working with pointers in c/c++

How to avoid this issue?

Make sure to know how many pointers you have pointing to each memory address. When you assign a new value to a pointer, release the memory if there is only one pointer with a reference to that memory address.

If you release the data in that specific memory address, and there is another pointer pointing to it, then you will have the first problem, a null reference.

Summary

Pointers are a powerful tool that every C/C++ programmer have to know. 

As usual, with power comes responsibility. So you have to know how to handle them properly.

Specifically, you want to avoid the two main problems you can face while working with pointers. If you avoid them, you will have more than 90% of your problems (with c/c++) solved.

If you use them wisely, you will create fast programs. Programs that in other programming languages can be slow.

H@appy coding!