Pointers

Performance: Pointers vs References

What are the performance implications of using pointers vs. references in C++?

3D character art

When it comes to performance, the difference between pointers and references in C++ is often negligible in most scenarios. However, there are some subtle differences and situations where one might be preferred over the other. Let's break this down:

Under the Hood

At the assembly level, pointers and references are often implemented similarly. A reference is typically implemented as a constant pointer that is automatically dereferenced when used. This means that in many cases, the performance difference is minimal or non-existent.

Pass-by-Reference vs Pass-by-Pointer

Both can be used to avoid copying large objects when passing them to functions:

#include <chrono>
#include <iostream>

struct LargeObject {
  int data[10000];
};

void modifyByPointer(LargeObject* obj) {
  obj->data[0] = 42;
}

void modifyByReference(LargeObject& obj) {
  obj.data[0] = 42;
}

int main() {
  using namespace std::chrono;
  LargeObject obj;

  auto start = high_resolution_clock::now();
  for (int i = 0; i < 1000000; ++i) {
    modifyByPointer(&obj);
  }
  auto end = high_resolution_clock::now();
  std::cout << "Pointer time: "
    << std::chrono::duration_cast<microseconds>(
      end - start).count()
    << " microseconds\n";

  start = high_resolution_clock::now();
  for (int i = 0; i < 1000000; ++i) {
    modifyByReference(obj);
  }
  end = high_resolution_clock::now();
  std::cout << "Reference time: "
    << std::chrono::duration_cast<microseconds>(
      end - start).count()
    << " microseconds\n";
}

In this example, you'll likely find that the performance difference is negligible.

Null Checking

Pointers allow for null checking, which can introduce a small performance overhead:

// This check introduces a small overhead
void processCharacter(Character* character) {
  if (character) {
    character->doSomething();
  }
}

// No null check possible
void processCharacter(Character& character) {
  character.doSomething();
}

References, on the other hand, are assumed to always be valid, which can lead to slightly better performance in scenarios where null checks aren't necessary.

Polymorphism

Both pointers and references support polymorphism, allowing for runtime polymorphic behavior:

#include <iostream>

class Weapon {
 public:
  virtual void attack() = 0;
};

class Sword : public Weapon {
 public:
  void attack() override {
    std::cout << "Sword attack!\n";
  }
};

void useWeapon(Weapon* weapon) {
  weapon->attack();
}

void useWeapon(Weapon& weapon) {
  weapon.attack();
}

int main() {
  Sword sword;
  useWeapon(&sword);  // Using pointer
  useWeapon(sword);   // Using reference
}
Sword attack!
Sword attack!

The performance difference here is typically negligible.

Conclusion

In most cases, the performance difference between pointers and references is minimal. The choice between them should generally be based on semantics rather than performance:

  • Use references when you have an object that must exist and cannot be null.
  • Use pointers when you need to represent the absence of an object (null) or when you need to change what the pointer points to.

Modern compilers are very good at optimizing code, often eliminating any performance differences between pointers and references. Focus on writing clear, maintainable code, and choose between pointers and references based on your specific needs and the semantics of your program.

Answers to questions are automatically generated and may not have been reviewed.

3D art showing a progammer setting up a development environment
Part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

This course includes:

  • 60 Lessons
  • Over 200 Quiz Questions
  • 95% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

View Course
Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2025 - All Rights Reserved