Home > Articles > Programming > C/C++

Choosing Between References and Pointers in C++

  • Print
  • + Share This
Pointers have always been among the favorite subjects of C and C++ programmers. The introduction of reference types to the C++ language raises a series of questions: How are references similar or different from pointers? How are they implemented? And how do they replace pointers altogether in the modern crop of languages: Java, C#, and VB.NET? Also, can you reduce pointer usage in C++? Brian Overland, author of C++ Without Fear: A Beginner's Guide That Makes You Feel Smart, 2nd Edition, explores all these points.
Like this article? We recommend

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 statement—references are pointers without pointer syntax—is 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 languages—including 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 variables—p1, p2, and p3—each 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.

Moreover—and I will go out on a limb to say this—to 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 language—which C++ is nearly backward-compatible with—was 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 why—especially with 2D arrays—array 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 languages—Java, C#, and VB.NET—don’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 uses—you guessed it—a 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 object—an instance of a class—in 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 fine—as 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.

  • + Share This
  • 🔖 Save To Your Account

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020