What exactly is a pointer?

A pointer is a variable which “points” to an address in computer memory. This is a ‘reference’ to an object of some type.

At the hardware level, everything is stored in memory within blocks. Each variable has a memory address. For example, if you create an integer, like so:



int MyVariable = 10;


you will store the value “10”, somewhere in your RAM.

If you want to know exactly where in your RAM this value is stored, you can get the memory address by using the “&” symbol, like so:



&MyVariable


This will give you something like:
0xC0ABCD13

which is a hexadecimal representation of a 32 bit memory address.

Let’s review: Every variable has both a memory address and a data value.

Stored at address 0xC0ABCD13 is the value 10.

A pointer is a variable which stores memory addresses! Since it’s a variable, your pointer be reassigned to point to many different addresses over the course of a program.

Now, here’s where things may get a bit confusing, so read this over until it makes sense…

Since a pointer stores only a memory address, such as 0xC0ABCD13, the computer doesn’t know what kind of data is at that address. When you assign your pointer a variable type, such as ‘int’ or ‘AActor’, you’re giving the computer a hint at the variable type it should expect to find at that address space. You can do something called ‘dereferencing’ a pointer, which means you’re grabbing the data stored at a memory address rather than the memory address itself. Since you gave your pointer a data type, it knows how to grab and handle that data (‘treat it like an integer’ vs. ‘treat it like an AActor’).

The pointer dereferencing syntax uses an asterisk:
int data = *MyVariable;

When you’re pointing to a class which contains its own variables, you can access those variables with a “->” operator. This is much like the “.” dot operator you’d use for structs, but is really just a shorthand syntax.

float CharMoveSpeed = MyChar->MoveSpeed;

The “->” operator is actually doing this:
float CharMoveSpeed = *(&MyChar).MoveSpeed;

So, what happens if you try to dereference a pointer address which is invalid? What if the pointer address is NULL (or 0x00000000) because it was uninitialized?
The computer will try to access the empty pointer and it will try to convert the data it finds at address 0x00000000 to your pointer type. It will fail and you will get a “NULL reference exception”. This is the most common type of error you will get when a program crashes! So, before you dereference a pointer, always make sure that it is valid.

Here’s where things can get interesting:
Let’s suppose you have 5 pointers, all pointing to the same memory address:



int MyData = 10;
int *p1 = MyData;
int *p2 = MyData;
int *p3 = MyData;
int *p4 = MyData;
int *p5 = MyData;


If you read the data value pointed to by all these pointers, they will all return the value “10”. Now, let’s change it!



*p1 = 20;


Now, if you read the data value pointed to by all of these pointers, they will all return the value “20”.

3 Likes