Choosing Between References and Pointers in C++
Documentation is not always written with the elegance of William Shakespeare or the clarity of Carl Sagan. I know that better than anyone, as I used to write some of it, and made myself very unpopular with my fellow writers when I complained about it!
Years ago, I struggled with the documentation in Visual Basic 2.0 on creating objects (don’t worry; we’ll get to C++ in a moment). It kept saying, “In VB, objects are references.” I knew that references were also used in C++, but mainly for copy constructors (or so I thought). As I said, this was a number of years ago.
After an hour of banging my head against the wall, a light bulb finally went on. I realized, “Hey, references are just pointers without the pointer syntax! Now I understand!”
That statementreferences are pointers without pointer syntaxis not precisely true. But the relationship between references and pointers is often a close one. It turns out that most of the newer, popular programming languagesincluding VB.NET, Java, and C#take away pointers altogether and require you to use references in their place.
C++ retains both. To write the best C++, code, you need to know where best to use one or the other and what, if anything, they have to do with each other.
Pointers and References: A Review of Fundamentals
In C and C++, the best way to understand pointers (assuming you’re not already fluent in machine code, in which case they’re easy) is to understand that a pointer is a variable providing access to another piece of data. It’s easier to understand with an example.
double x = 3.14; double *p1 = &x; double *p2 = &x; double *p3 = &x;
In this example, p1, p2, and p3 are pointers, and they are all initialized with the address x, denoted as &x. Consequently, all changes to any of the variables *p1, *p2, *p3 result in changes to x, and all uses of *p1, *p2, and *p3 get the value of x[el] because *p1 just means “the thing that p1 points to.”
For example, consider these statements:
*p1 = (*p2 / 2) + *p3; (*p3)++;
These statements have the same effect as:
x = (x / 2) + x; x++;
Only one floating-point variable (type double) is allocated in this example, but it can be referred to in a number of different ways, almost as if it had three other names (*p1, *p2, *p3).
We can do something similar with reference variables. In this example, aRef, bRef, and cRef all refer to the variable x. The effect in this case is to literally give x three other names.
double x = 3.14; double &aRef = x; double &bRef = x; double &cRef = x;
These declarations create just one floating-point variable, x, but provide three other ways to refer to it, so that the statements
aRef = (bRef / 2) + cRef; aRef++;
have exactly the same effect as
x = (x / 2) + x; x++;
References, therefore, have a function similar to that of pointers, but already some differences are apparent: The example with pointers mandates the creation of three additional variablesp1, p2, and p3each of which holds an address (32 bits on most computers). It’s hard for me to imagine that any compiler would refuse to allocate the pointer variables.
In contrast, with the reference example, it’s possible the compiler might allocate three pointers and hide that fact, turning the use of aRef, for example, into a dereferenced pointer.
Yet a really smart optimizing compiler wouldn’t even do that. It would just recognize that the use of aRef, bRef, or cRef in this example is really the same as using x itself. So it wouldn’t bother to allocate any extra data.
Where They Get Useful: Function Arguments
Of course, until you start using them in functions, both pointers and references usually seem to have little, er, point. The classic use of pointers is in a swap function. Suppose you want to write a function that has the potential to operate on its arguments, permanently changing their values. You will need to use pointers or references. Here’s the pointer version:
void swap_with_ptrs(int *p1, int *p2) { int temp = *p1; *p1 = *p2; *p2 = temp; }
Here is the version that uses references:
void swap_with_refs(int &aRef, int &bRef) { int temp = aRef; aRef = bRef; bRef = temp; }
These two functions look similar and in fact do the same thing.
I’ve gotten into trouble with one or two people by saying this, but in my opinion, both of these version implement what, in other languages, you would call pass by reference; they do what in BASIC or FORTRAN you would do by specifying a reference variable, which lets the function or subroutine change the value of the argument passed to it. In C, using pointers was the only way you could “pass by reference.”
Technically, what’s really going on with the pointer version is that it’s passing pointers by value, but the effect is exactly the same as passing integers by reference.
Moreoverand I will go out on a limb to say thisto implement the reference version, the compiler will almost certainly use pointers and de-reference them under the cover. Why? Because there’s no other way to do it, at least no obvious way.
But... if the two versions produce exactly the same effects at runtime, how do you choose between them?
I’m going to come down on the side of using references, because even though the implementations are identical, there is one other difference: When you call the function at runtime, the version with references passes the variables directly, rather than making you use the additional operator needed to get the addresses (&). So, it involves slightly less syntax.
int big = 100; int little = 1; swap_with_ptrs(&big, &little); // swap big and little swap_with_refs(big, little); // swap again!
References were originally added to the C++ language to make it easy and efficient to write copy constructors. But, in addition for functions like swap, I would recommend using references as much as possible rather than pointers.
Pointers aren’t going to go away any time soon, because C++ code often contains thousands of lines of C legacy code. Furthermore, C++ programmers are just in the habit of using them.
Another Use of Pointers: Efficient Array Processing
The C programming languagewhich C++ is nearly backward-compatible withwas originally developed for writing operating systems. As such, it had to enable the programmer to write as close to the metal as possible, producing code nearly as efficient as assembly or machine code.
In the old days of computing, it was interesting, even exciting, to see how use of pointers could speed up your programs. In the 1980s, I honed my C-language programming skills by writing several different versions of Conway’s Game of Life. In the first version, I processed two-dimensional arrays the obvious way, with array indexes. For example, to zero-out an array:
int i = 0; int j = 0; for (i = 0; i < NROWS; i++) { for (j = 0; j < NCOLS; j++) { grid[i][j] = 0; } }
(Note: this being ancient C code, I couldn’t declare i or j inside the loops.)
Here was the version using a pointer to do the same thing but far more efficiently:
int **p = grid; int **end = grid + NROWS + NCOLS; for (p = grid; p < end; p++) { **p = 0; }
By using this optimization, along with a few other tricks, I sped up the Game of Life by a factor of ten! At first the gliders, pulsars, floaters, and other “organisms” were moving like snails; but after my optimizations, they were zipping across the screen at blinding speed. The gliders were moving so fast I could hardly see them! I had to start adding in delay mechanisms to control the speed.
You should be able to see whyespecially with 2D arraysarray processing was so much faster with pointers. Rather than laboriously calculating each cell positions by using indexes, the pointer version zipped through the array by incrementing one integer position after each iteration until reaching “end,” an address that was calculated just once, ahead of time.
Incidentally, a decent optimizing compiler would have done that calculation for me, but I took the task upon myself. I should’ve been able to write:
int **p = grid; for (p = grid; p < grid + NROWS + NCOLS; p++) { **p = 0; }
The problem with this use of pointers for efficiently processing arrays is that it has become largely superfluous. In accordance with Moore’s Law, processors have become thousands of times faster than in the 1980s. Memory is far more plentiful and much cheaper, so that allocation of an extra variable here or there should hardly concern you anymore.
The bottom line is that unless you’re writing part of an operating system or device driver with a piece of code designed to be executed millions of times a second, you are hardly ever going to feel the difference between arrays processed with indexes versus arrays processed with pointers.
And that is why the newer generation of languagesJava, C#, and VB.NETdon’t support pointers at all. The philosophy behind these languages is that any code-optimization you would get from using a pointer to process an array is a trivial matter given today’s hardware. Instead, you use array indexes, and those indexes are automatically checked to see if they are in bounds.
Safety first is the philosophy of these languages.
All these language also support a for each statement (also called “ranged-based for”), which the C++11 specification now supports as well. The beauty of this feature is that it keeps array references strictly under the control of the compiler, not permitting i to go out of bound, but it also permits the compiler to internally optimize array-access technique for greatest efficiency. With C++11, here’s how I’d zero-out my array:
for (int &i : grid) { i = 0; }
This version doesn’t use pointers. But note that it usesyou guessed ita reference variable.
C++ Pointers and new
I can think of only one significant area in which it looks like pointers are never going to go away for C++ programmers, at least not any time soon. When you use the new keyword, either to dynamically allocate an object or dynamically allocate a primitive data item (integer or floating-point), the keyword returns an address, which you normally assign to a pointer.
int *p1 = new int; // p points to a dyn. allocated integer
One thing I like about C++ is that, unlike Java, C#, and VB.NET, it recognizes no fundamental difference between primitive types and classes. Classes, in C++, are simply types that you create yourself, in effect extending the language.
Therefore, you dynamically allocate and refer to an objectan instance of a classin precisely the same way you’d dynamically allocate an integer:
MyClass *p2 = new MyClass; // p points to a dyn. allocated object
But this syntax mandates pointer usage; I can see no safe way of coercing such a pointer value into a reference. You are stuck with pointers, at least in current, standard versions of C++. (Because, as shown earlier, pointers and references in function arguments usually have the same underlying implementation. It would be theoretically possible, I suppose, to trick the compiler into accepting one for the other, but don’t ever try that at home!)
Interestingly enough, Java, C#, and VB.NET all use references in this situation, which is relevant to my learning problem that I mentioned at the beginning of this article. In these languages, you can declare an object variable (that is, a variable with class type), but it is always a reference. That means it “points nowhere” unless initialized with either (1) an object allocated with new or (2) the name of an existing object. Because references are used consistently in this context, they work just fineas long as you understand how references work.
There is one other reason why you will continue to work with pointers in C++. The use of pointers is deeply ingrained in the C++ standard library inherited from C. Many of the newer language features, such as range-based for and the items in the Standard Template Library (including the C++ string class) tend to minimize pointer usage, and in doing so present far fewer opportunities for you to inadvertently shoot yourself in the foot.
And yet C++ will always support pointers because, despite their dangers and difficulties, there will always be that occasional op-system or device-driver writer that needs them.