If you want to code games in Unreal Engine 4 in C++, in order to get a firm grip and be fluent you must make use of pointers. Usually, newcomers in C++ are afraid of pointers because they think that they have no actual functionality and are hard to master. Well, truth is that pointers can be used:
- To iterate through an array
- To pass a large amount of data to a function in a way that doesn’t involve copying the data, which is inefficient
- To pass a function as a parameter to another function
- To achieve polymorphism when dealing with inheritance
- To have one struct or class point at another struct or class in order to form a chain
This post does not include code written in Unreal Engine – if you want to see how pointers work in Unreal Engine check out this post. In order to use pointers inside Unreal Engine you should have a basic understanding of pointers so you may avoid hours of frustration and demotivation.
For this post, I’m going to create a console application in C++ using Visual Studio and explain with simple code what pointers are.
Before I start explaining what exactly is a pointer, you should be familiar with:
- The Address-of operator “&”
- The Dereference operator “*”
In this post, I will first explain the above operators and then I’m going to show you what pointers really are.
Setting up the Visual Studio Project
As I said above, for this post, I’m going to create a console application in C++ using Visual Studio. In order to catch up with this post follow the steps below:
- Open Visual Studio
- Click on the New Project menu
- Select Visual C++
- Select Win32 Console Application
- Give a name to your project from the name menu
- Click OK
- In the menu that pop ups select Finish
Note: The default c++ console application from Visual Studio 2013 to 2015 tend to differ. In this example, both versions are covered with screenshots. However, you need to have Visual Studio 2015 in order to create Unreal Engine projects using the version 4.10 and above.
When Visual Studio loads your project, make sure to:
- Include the “iostream” library after the “stdafx.h”
- Before the main function, type: using namespace std;
I included that library and use that namespace in order to output messages into the console. Below, I have provided a screenshot from my code right now:
Notice the different main signatures.
Understanding the address-of operator “&”
The address-of operator “&” allows us to see the actual memory address which is assigned to a variable.
To see that, I declared an integer named x and printed the value of x as well as the address-of x making use of the address-of operator by typing the following code:
When the program is executed, your output should be similar to the image below:
Note that each time you execute your program, the second output is going to change. This is because each time the compiler stores your variable in a different actual memory address. With that said, your address is more than likely to differ than mine.
Understanding the dereference operator “*”
The dereference operator “*” allows us to get the actual value of a particular address. Right after the output of the address-of x type the following:
cout<<*&x<<“\n”; //Print the value of the address of x
Your end code with the output, should be similar to the image below:
Since the dereference operator returns the actual value of a particular address it’s only logical to return 1 in this case. Also, notice that the address value is not the same as above but that’s ok! Moreover, the above screenshot was taken from Visual Studio 2015, if you are following this example with a previous version your main function might differ but your output should be similar.
Now that these operators are clear it’s time to define what a pointer really is. A pointer is essentially a variable that holds a memory address as its value. Now you might already thinkink how you can use the above operators. Let’s see!
To declare a pointer you need to tell the compiler what type of pointer you want to create. That means that you need to tell the compiler, for example, that a pointer points to a memory address in which an integer value is stored.
In order to declare a pointer which points to an integer value you can type:
int* IntPtr; //This is a pointer which points to an integer value
Warning: The “*” symbol is not a dereference operator – it’s just a way to declare that IntPtr is a pointer which points to an integer value.
Using the same method as above, I declare a pointer which points to a double value with the following way:
double* DoublePtr; //This is a pointer which points to a double value
Note: It doesn’t matter if you type the asterisk next to int or in between “int” and “IntPtr” or right before “IntPtr”, however in Unreal Engine official naming conventions it is suggested that you put the asterisk next to the type of pointer that you declare which is the approach we are following right now.
My code right now is described in the following image:
Since I just declared these pointers, they contain garbage addresses. This means that I need to assign a value to them in order to use them.
In order to assign a value to a pointer, after its declaration, type:
IntPtr=&x; //Assigning the address-of x to the IntPtr pointer
In order to output the value of that pointer, after the assignment type in the following code:
cout << “IntPtr contains: ” << *IntPtr << “\n”; //Write the value of the pointer
My code with the end result is described below:
To sum-up pointers are variables that point to a memory address. They can be dereferenced using the dereference operator “*” to retrieve the value at the address they are pointing.
Warning: Dereferencing a pointer which points to garbage memory will crash your application / game.
In order to avoid that, you can include the following “if” statement: