Intro to Pointers – Game Hacking Guide

Before we go into this Intro to Pointers, lets talk about variables and memory. A variable is something that has a value that can vary through-out a programs execution and it is stored in memory.

Memory, think of it like a notebook. When your program starts it'll fill in the notebook with any data it knows its going to need at the start. This is stuff like any hard-coded values or "strings" in your program will be written in there at the start. There is a pattern to how and where it writes pieces of data depending on how they are declared in the program but I won't be going into such details. What matters is that the program stores all this information in the notebook/memory.

et_trustzone_download_anonymously_3_slides

Intro to Pointers a Game Hacking Guide

Every variable you create has the value assigned to it stored somewhere in this notebook.

Intro to Pointers - Game Hacking GuideSo, say you wanted to show your friend this really awesome number you stored in one of your variables. You have two options, you can simply tell your friend the number: "Thirty-one is a cool number!", or you can tell them where to find the number in your notebook: "Check the first line of page four"

In the first case when you just tell your friend the number you're basically duplicating the data it works great for small stuff, but what if instead of a number you wanted your friend to proof-read your paper. You could read it out to them, but then any changes they try to make will be on their copy of it. Instead you'd want to show them where the paper to be proofread is in your notebook and they can make changes to it there.

In these examples, a pointer is the second case. A pointer is just that, it points you to where something is stored. In the case of your program, a pointer is just a number that corresponds to where in your programs memory space(notebook) the value begins.

So now you know what a pointer is, but pointer types are still a little tricky. We'll go over some of these differences in this Intro to Pointers Game Hacking Guide...

int x = 5;
int* ptr = &x;//& means the address of, so &x means the address of the variaible x

In the above code we have the variaible x which gets the value 5. We also have another line though, its a pointer type but its also a variaible. That means the value we assign to ptr gets stored somewher in memory also. It just so happens that at this place in memory we are storing the address to another piece of memory. Remember, the address to a computer is just a number that happens to correspond to a place in memory, so int* ptr is  storing a number just like int x is. The only special thing is that we told the compiler that the number we store in ptr is special as it points to where something else is stored. Otherwise the int* and int are pretty much the same.

int x = 5;
int* ptr = &x;
int** ptr2 = &ptr;
int*** ptr3 = &ptr2;
...

We can carry this on, since int* is just a variable storing a value, int** ptr2 is just a variable storing a value also, its value happens to be the address of the int* ptr which happens to be the address of int x. At the core, pointers are just normal ole integers.

So why do we even call them pointers if they work just like normal variables? Well we do that so the compiler understands the type of data we have. When it knows we are working with a pointer to somewhere in memory we can instruct it to operate on the data being pointed to:

int x = 5;
int* ptr = &x;
*ptr = 22;

That code, because the compiler understands ptr is a pointer, when we tell it to access *ptr and assign it a value it knows to actually operate on the memory being pointed to by ptr and not the value of ptr itself.

Lets consider some example code for a game;

#include <stdio.h>

void left(int x, int y) {
    x = x - 1;
}
void right(int x, int y) {
    x = x + 1;
}
void up(int x, int y) {
    y = y + 1;
}
void down(int x, int y) {
    y = y - 1;
}

int main() {
    int x = 10;
    int y = 10;
    printf("You are now at (%d,%d)\n",x,y);
    left(x,y);
    left(x,y);
    up(x,y);
    right(x,y);
    down(x,y);
    printf("You are now at (%d,%d)\n",x,y);
}

After this code runs, what should it print out? If you guessed

You are now at (10,10)
You are now at (10,10)

Then you're correct, that is because the x and y the movement functions are acting upon are copies of the original values. If we want to work on the original values we need to give the functions pointers to where they are stored. That way the function itself can modify the players x and y coordinates.

#include <stdio.h>

void left(int* x, int* y) {
    *x = *x - 1;
}
void right(int* x, int* y) {
    *x = *x + 1;
}
void up(int* x, int* y) {
    *y = *y + 1;
}
void down(int* x, int* y) {
    *y = *y - 1;
}

int main() {
    int x = 10;
    int y = 10;
    printf("You are now at (%d,%d)\n",x,y);
    left(&x,&y);
    left(&x,&y);
    up(&x,&y);
    right(&x,&y);
    down(&x,&y);
    printf("You are now at (%d,%d)\n",x,y);
}

This time the functions take pointers to the int so they can move the player on their own. So what should the output be?

You are now at (10,10)
You are now at (10,9)

SystemMechanic-1Of course you might be thinking what don't I just utilize return values?

x = left(x,y);
y = up(x,y);
[code]

Of course that will work in these simple cases, but what if I wanted to implement diagonal moves?

[code]
??? upleft(int x, int y) {
    x = x - 1;
    y = y + 1;
    return ???
}

What do you return then? If you use pointers this isn't a problem you can pass the function all the data it needs to operate on

void upleft(int* x, int* y) {
    *x = *x - 1;
    *y = *y + 1;
}
upleft(&x,&y);

Pointers made it possible to cleanly do this.

This isn't a very complete guide to pointers, more of a Intro to Pointers. There are many more nuances when you start dealing with arrays vs pointers and their differences but this should hopefully give you an understanding of what a pointer is and how they can be used.

 

Intro to Pointers a Game Hacking Guide written by dropzone, all credit to him for this amazing guide.

Leave a Reply

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

s2Member®