DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • 4 Expert Tips for High Availability and Disaster Recovery of Your Cloud Deployment
  • Using OpenAI Embeddings Search With SingleStoreDB
  • Execution Type Models in Node.js
  • What ChatGPT Needs Is Context

Trending

  • 4 Expert Tips for High Availability and Disaster Recovery of Your Cloud Deployment
  • Using OpenAI Embeddings Search With SingleStoreDB
  • Execution Type Models in Node.js
  • What ChatGPT Needs Is Context

Pointers vs References

Steve Francia user avatar by
Steve Francia
·
Jul. 03, 14 · Interview
Like (0)
Save
Tweet
Share
5.74K Views

Join the DZone community and get the full member experience.

Join For Free

Some languages including C, C++ support pointers. Other languages including C++, Java, Python, Ruby, Perl and PHP all support references. On the surface both references and pointers are very similar, both are used to have one variable provide access to another. With both providing a lot of the same capabilities, it’s often unclear what is different between these different mechanisms. In this article I will illustrate the difference between pointers and references.

Why Does This Matter

Pointers are at the very core of effective Go. Most programmers are learning Go with a foundation in one of the languages mentioned above. Consequently understanding the difference between pointers and references is critical to understanding Go. Even if you are coming from a language that uses pointers, Go’s implementation of pointers differs from C and C++ in that it retains some of the nice properties of references while retaining the power of pointers.

The remainder of this article is written with the intent of speaking broadly about the concept of references rather than about a specific implementation. We will be using Go as the reference implementation for pointers.

What Is The Difference?

A pointer is a variable which stores the address of another variable.

A reference is a variable which refers to another variable.

To illustrate our point, use the following example in C++ which supports both pointers and references.

int i = 3;
int *ptr = &i;
int &ref = i;

The first line simply defines a variable. The second defines a pointer to that variable’s memory address. The third defines a reference to the first variable.

Not only are the operators different, but you use the differently as well. With pointers must use the * operator to dereference it. With a reference no operator is required. It is understood that you are intending to work with the referred variable.

Continuing with our example, the following two lines will both change the value of i to 13.

*ptr = 13;
ref = 13;

You may be asking, what happens if I try to access the ptr directly without dereferencing first. This takes us to our second critical difference between pointers and references. Pointers can be reassigned while references cannot. In other words, a pointer can be assigned to a different address.

Consider the following example in Go:

package main

import "fmt"

var ap *int

func main() {
    a := 1          // define int
    b := 2          // define int

    ap = &a
     // set ap to address of a (&a)
     //   ap address: 0x2101f1018
     //   ap value  : 1

    *ap = 3
     // change the value at address &a to 3
     //   ap address: 0x2101f1018
     //   ap value  : 3

    a = 4
     // change the value of a to 4
     //   ap address: 0x2101f1018
     //   ap value  : 4

    ap = &b
     // set ap to the address of b (&b)
     //   ap address: 0x2101f1020
     //   ap value  : 2
}

So far you could do all of the above in a reasonably similar manner using references, and often with a simpler syntax.

Stay with me, the following example will illustrate why pointers are more powerful than references.

Extending the function above:

    ...

    ap2 := ap
     // set ap2 to the address in ap
     //   ap  address: 0x2101f1020
     //   ap  value  : 2
     //   ap2 address: 0x2101f1020
     //   ap2 value  : 2

    *ap = 5
     // change the value at the address &b to 5
     //   ap  address: 0x2101f1020
     //   ap  value  : 5
     //   ap2 address: 0x2101f1020
     //   ap2 value  : 5
    // If this was a reference ap & ap2 would now
    // have different values

    ap = &a
     // change ap to address of a (&a)
     //   ap  address: 0x2101f1018
     //   ap  value  : 4
     //   ap2 address: 0x2101f1020
     //   ap2 value  : 5
    // Since we've changed the address of ap, it now
    // has a different value then ap2
}

You can experiment and play yourself at go play:http://play.golang.org/p/XJtdLxFoeO

The key to understanding the difference is in the second example.

If we were working with references we would not be able to change the value of b through *ap and have that reflected in *ap2. This is because once you make a copy of a reference they are now independent. While they may be referring to the same variable, when you manipulate the reference it will change what it refers to, rather than the referring value.

The final example demonstrates the behavior when you change the assignment of one of the pointers to point to a new address. Due to the limitations of references this is the only operation available.

Stay tuned… Next post will feature another property exclusively available to pointers, the pointer pointer.

For more information on pointers I’ve found the following resources helpful

  • understanding pointers and memory allocation
  • Pointers in Go
  • Pointers
Pointer (computer programming)

Published at DZone with permission of Steve Francia, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • 4 Expert Tips for High Availability and Disaster Recovery of Your Cloud Deployment
  • Using OpenAI Embeddings Search With SingleStoreDB
  • Execution Type Models in Node.js
  • What ChatGPT Needs Is Context

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: