DEV Community

Cover image for Understanding C++ Pointers: The Power Behind the Address
Arto Baltayan
Arto Baltayan

Posted on

Understanding C++ Pointers: The Power Behind the Address

Why Raw Pointers Still Matter

If you truly want to understand what's going on under the hood, you
need to understand raw pointers.

If you are a C++ developer, you've probably been encouraged to use smart
pointers like std::unique_ptr or std::shared_ptr. These are powerful
abstractions and, quite frankly, much safer to use in most cases.
However, they are still built on top of raw pointers.

If you truly want to understand what's going on under the hood, you need
to understand raw pointers. You may never need to use them directly, but
learning them gives you a solid foundation and a clearer understanding
of what's really happening.

Back when I was learning to code, raw pointers were the only option
available to us. To use them effectively, you had to become disciplined
about memory management. You had to think carefully about how and when
memory was allocated and destroyed on the heap. If you didn't get this
right, that's when you ended up with memory leaks.

Knowing how to use pointers—and how memory is manipulated—isn't
just about syntax. It's about understanding the machine.

So that brings us to the question: what is a pointer?

Simply put, a pointer is just another variable, like an int or a
float. However, this kind of variable is special because of what it
stores. Instead of holding a direct value, it stores the address of
another variable in memory. When a pointer holds the address of a
specific location, it is said to be "pointing" to that location. Using
special symbols, we can access the data stored there.

Let's take a closer look at this.

int x = 10;
int* ptr = &x;
Enter fullscreen mode Exit fullscreen mode

Here's what's happening:

  • x is an integer variable that holds the value 10
  • &x means "the address of x"
  • ptr is a pointer variable used to store the address of x
  • int* placed in front of ptr declares it as a pointer to an integer

Depending on the context, the * symbol can mean two different things.
In a declaration, like above, it indicates that a variable is a pointer.
In other contexts, it is used to dereference the pointer—that is, to
access the value stored at the memory address it points to.

In other words, it allows us to access x, like so:

std::cout << *ptr;
Enter fullscreen mode Exit fullscreen mode

The line above prints the value 10. As you can see, by using the
pointer, we accessed and displayed the value stored in x.


What Are Pointers Used For

Pointers are used when you need direct control over memory or when
working with structures that rely on relationships between objects.

1. Dynamic Memory Allocation

int* p = new int(5);
Enter fullscreen mode Exit fullscreen mode

This allocates memory similar to the earlier example, but with one
important difference. The keyword new allocates memory on the heap
(delete deallocates it). In this case, it allocates enough space to
hold an integer, places the value 5 into that memory, and assigns the
address to the pointer p.

After this statement executes, you have an integer stored on the heap
with the value 5, and p points to it.

Note that p is a pointer to an integer. This is necessary because it
must store the address of a memory location that holds an integer.

2. Working with Data Structures

Complex structures like lists, trees, and graphs all rely on pointers to
connect their nodes.

struct Node {
    int data;
    Node* next;
};
Enter fullscreen mode Exit fullscreen mode

This segment of code defines what's known as a self-referential
structure. The pointer inside it—called next—can hold the memory
address of another Node. In other words, each node can point to
another node of the same type.

Without pointers like this, data structures such as linked lists and
binary search trees could not exist.

3. Interfacing with Low-Level Systems

Operating systems, embedded systems, and performance-critical
applications often require direct memory access and control. Pointers
provide that level of control.

A good example is game development, where performance and memory
efficiency are critical.


How Pointers Work (The Basics)

When you declare:

int x = 10;
Enter fullscreen mode Exit fullscreen mode

The system stores the integer value 10 at some location in memory. For
the sake of illustration, let's say the address where this value is
stored is 0x1000.

Now, when you write:

int* ptr = &x;
Enter fullscreen mode Exit fullscreen mode

This statement is saying: take the memory address of x and store it in
ptr. In other words, make ptr point to x. At this point, ptr
contains the value 0x1000.

When you dereference ptr using the * operator, you are accessing the
value stored at the memory address that ptr holds—in this case, the
value inside x.

*ptr = 20;
Enter fullscreen mode Exit fullscreen mode

This statement does not change the pointer itself. It changes the value
at the memory location the pointer is referring to. In this example, it
replaces the 10 stored in x with 20.

Key distinction:

  • ptr by itself represents the address (for example, 0x1000)
  • *ptr represents the value stored at that address (the same value stored in x)

That distinction—between the address and the value at the
address—is the key to understanding how pointers work.


A Final Thought

Even though it may sometimes seem that way, there's nothing magical
about pointers. They are simply variables that can do very powerful
things. Keeping that in mind is half the battle.

That said, there are important things you need to pay close attention
to. You must be disciplined when working with raw pointers. Mistakes,
carelessness, or poor memory management can lead to undefined behavior.
There's a real sense of responsibility that comes with using these
variables—but that responsibility is also what makes them so powerful.

Understanding raw pointers means understanding how C++ really works.

And once you understand that, everything else in the language starts to
make more sense.


See Pointers in Practice

If you'd like to see pointers used in a real project, I use them
throughout my Binary Search Tree implementation. The repository includes
the full C++ source code, along with supporting documentation and an API
reference that walks through the design and behavior of the structure.

GitHub repository:

https://github.com/arto-b/BST_Demo

Top comments (0)