Pointers and Memory

Computer Memory and Pointers

A computer’s memory is nothing more than a giant array. If you have eight gigabytes of RAM, it only means you have a gigantic eight-billion element array with each element being one byte. It’s the operating system’s job to manage this giant array, handing out small chunks of it to programs that need it. A pointer is an index on this giant array. In other words, pointers are just numbers.

To declare a pointer variable, simply add a * next to the type name. For example, if you want an int pointer named x, simply write int* x. Note that in some programs, you might see int *x (note the different spacing), but that’s basically the same.

It’s also possible to declare pointers to other pointer types. For example, int** x is just a pointer for the int* type.

Pointer Operations

The most basic operation for working with a pointer is the * operator. Roughly, if you have a pointer p, then *p is just the value at memory address p. The pointer’s type determines how much of the memory it reads. If you have an int pointer, it will look at the next 4 bytes from that memory address. If you have a char pointer, it will only look at one byte. Later on we’ll look at defining our own data types, and you can expect the * operator to read the correct number of bytes that correspond to our custom data types.

The other interesting operator right now is the & operator. The & operator takes a variable and gives us the memory address of that variable. In other words, & returns a pointer to something. In case you’ve been wondering, this is how scanf works and why we have to put & beside the variables we want scanf to save to. Functions cannot modify the variables that are passed to it, but if it has the correct memory address, it can just write data directly to that memory address. And that is why scanf needs us to give the memory addresses to save into.

Here is a program that uses pointers. Please compile and run it with the command line, and then study its output carefully.

#include <iostream>
using namespace std;

int main() {
long long c = 1000;

cout << "c is "   << (long long)c   << endl;
cout << "&c is "  << (long long)&c  << endl << endl;

long long d = 2000;

cout << "d is "   << (long long)d   << endl;
cout << "&d is "  << (long long)&d  << endl << endl;

long long* x = &c;

cout << "x is "   << (long long)x   << endl;
cout << "&x is "  << (long long)&x  << endl << endl;
cout << "*x is "  << (long long)*x  << endl;

long long** y = &x;

cout << "y is "   << (long long)y   << endl;
cout << "&y is "  << (long long)&y  << endl << endl;
cout << "*y is "  << (long long)*y  << endl;
cout << "**y is " << (long long)**y << endl;
}


Note that the code (long long)x is known as typecasting. If you don’t know what it is, we encourage you to read about it. You can think of it though as a conversion from one type to another. In this case, we’re converting everything to long long, which means that in the example (long long)&c we'll get to see the memory addresses themselves as integers.

An Experiment About Pointers as Numbers

Here is a fun little experiment to see what pointers are actually doing.

#include <iostream>
using namespace std;

int main() {
// The first byte of x is 3, the next is 5, then 7, then 11.
unsigned int x = (3 << 24) + (5 << 16) + (7 << 8) + 11;

// The unary & operator returns the memory address of a variable we already have.
// &x gives us the memory of x as an integer pointer.
// By the way, unary means single-argument. You might be more familiar with the unary minus operator.
// -a is a unary operator that returns the negative of a.
// So this takes the memory address of x and forcibly stores it in a pointer to a char (1 byte variable).
char* ptr = (char*)&x;

// Print out the data that ptr points to as an integer as a number
cout << "The value of ptr points to is: " << (int)*ptr << " and the memory address is " << (unsigned long long)ptr << endl;

// Make ptr point to the next byte. It can be thought of as an integer after all.
ptr++;

cout << "The value of ptr points to is: " << (int)*ptr << " and the memory address is " << (unsigned long long)ptr << endl;
ptr++;

cout << "The value of ptr points to is: " << (int)*ptr << " and the memory address is " << (unsigned long long)ptr << endl;
ptr++;

cout << "The value of ptr points to is: " << (int)*ptr << " and the memory address is " << (unsigned long long)ptr << endl;
}


The output that you get from this experiment depends on your computer’s processor. You will almost certainly get 11, 7, 5, then 3, but on rarer computers you will get 3, 5, 7, 11. This is the effect of something strange called Endianness, and we won’t be discussing that now. Actually, we likely won’t discuss it ever since it’s just a weird quirk of history. The important thing to remember is that memory is just one giant array, and we can cut it up however we like. The demonstration here is just a kind of silly way to play around with pointers.