Lesson 6: Pointers in C++: What Are Pointers? Why Should You Care?
Lesson 6: Pointers in C++: What Are Pointers? Why Should You Care?
By Alex Allain
Pointers are an extremely powerful programming tool. They can make some things much easier,
help improve your program's efficiency, and even allow you to handle unlimited amounts of data.
For example, using pointers is one way to have a function modify a variable passed to it. It is also
possible to use pointers to dynamically allocate memory, which means that you can write
programs that can handle nearly unlimited amounts of data on the fly--you don't need to know,
when you write the program, how much memory you need. Wow, that's kind of cool. Actually, it's
very cool, as we'll see in some of the next tutorials. For now, let's just get a basic handle on what
pointers are and how you use them.
The cool thing is that once you can talk about the address of a variable, you'll then be able to go
to that address and retrieve the data stored in it. If you happen to have a huge piece of data that
you want to pass into a function, it's a lot easier to pass its location to the function than to copy
every element of the data! Moreover, if you need more memory for your program, you can request
more memory from the system--how do you get "back" that memory? The system tells you where
it is located in memory; that is to say, you get a memory address back. And you need pointers to
store the memory address.
A note about terms: the word pointer can refer either to a memory address itself, or to a variable
that stores a memory address. Usually, the distinction isn't really that important: if you pass a
pointer variable into a function, you're passing the value stored in the pointer--the memory
address. When I want to talk about a memory address, I'll refer to it as a memory address; when I
want a variable that stores a memory address, I'll call it a pointer. When a variable stores the
address of another variable, I'll say that it is "pointing to" that variable.
Notice the use of the *. This is the key to declaring a pointer; if you add it directly before the
variable name, it will declare the variable to be a pointer. Minor gotcha: if you declare multiple
pointers on the same line, you must precede each of them with an asterisk:
// one pointer, one regular int
int *pointer1, nonpointer1;
// two pointers
int *pointer1, *pointer2;
As I mentioned, there are two ways to use the pointer to access information: it is possible to have
it give the actual address to another variable. To do so, simply use the name of the pointer
without the *. However, to access the actual memory location and the value stored there, use the
*. The technical name for this doing this is dereferencing the pointer; in essence, you're taking the
reference to some memory address and following it, to retrieve the actual value. It can be tricky to
keep track of when you should add the asterisk. Remember that the pointer's natural use is to
store a memory address; so when you use the pointer:
call_to_function_expecting_memory_address(pointer);
then it evaluates to the address. You have to add something extra, the asterisk, in order to
retrieve the value stored at the address. You'll probably do that an awful lot. Nevertheless, the
pointer itself is supposed to store an address, so when you use the bare pointer, you get that
address back.
For example:
#include <iostream>
int main()
{
int x; // A normal integer
int *p; // A pointer to an integer
The cout outputs the value stored in x. Why is that? Well, let's look at the code. The integer is
called x. A pointer to an integer is then defined as p. Then it stores the memory location of x in
pointer by using the address-of operator (&) to get the address of the variable. Using the
ampersand is a bit like looking at the label on the safety deposit box to see its number rather than
looking inside the box, to get what it stores. The user then inputs a number that is stored in the
variable x; remember, this is the same location that is pointed to by p.
The next line then passes *p into cout. *p performs the "dereferencing" operation on p; it looks at
the address stored in p, and goes to that address and returns the value. This is akin to looking
inside a safety deposit box only to find the number of (and, presumably, the key to ) another box,
which you then open.
Notice that in the above example, pointer is initialized to point to a specific memory address before
it is used. If this was not the case, it could be pointing to anything. This can lead to extremely
unpleasant consequences to the program. For instance, the operating system will probably prevent
you from accessing memory that it knows your program doesn't own: this will cause your program
to crash. To avoid crashing your program, you should always initialize pointers before you use
them.
It is also possible to initialize pointers using free memory. This allows dynamic allocation of array
memory. It is most useful for setting up structures called linked lists. This difficult topic is too
complex for this text. An understanding of the keywords new and delete will, however, be
tremendously helpful in the future.
The keyword new is used to initialize pointers with memory from free store (a section of memory
available to all programs). The syntax looks like the example:
int *ptr = new int;
It initializes ptr to point to a memory address of size int (because variables have different sizes,
number of bytes, this is necessary). The memory that is pointed to becomes unavailable to other
programs. This means that the careful coder should free this memory at the end of its usage.
The delete operator frees up the memory allocated through new. To do so, the syntax is as in the
example.
delete ptr;
After deleting a pointer, it is a good idea to reset it to point to 0. When 0 is assigned to a pointer,
the pointer becomes a null pointer, in other words, it points to nothing. By doing this, when you do
something foolish with the pointer (it happens a lot, even with experienced programmers), you
find out immediately instead of later, when you have done considerable damage.
In fact, the concept of the null pointer is frequently used as a way of indicating a problem--for
instance, some functions left over from C return 0 if they cannot correctly allocate memory
(notably, the malloc function). You want to be sure to handle this correctly if you ever use malloc
or other C functions that return a "NULL pointer" on failure.
In C++, if a call to new fails because the system is out of memory, then it will "throw an
exception". For the time being, you need not worry too much about this case, but you can read
more about what happens when new fails.