Swapping Values with Pointers

How can I use pointers to efficiently swap two values without using a temporary variable?

Swapping values using pointers without a temporary variable is an interesting technique, but it's important to note that it's not always the most readable or maintainable approach. However, it can be useful in certain situations where performance is critical. Here's how you can do it:

#include <iostream>

void swap(int* a, int* b) {
  // Check if the pointers are not the same
  if (a != b) {
    // XOR the values
    *a = *a ^ *b;
    // XOR again to get the original value of a
    *b = *a ^ *b;
    // XOR one more time to get the
    // original value of b
    *a = *a ^ *b;
  }
}

int main() {
  int x{5};
  int y{10};

  std::cout << "Before swap: x = "
    << x << ", y = " << y << "\n";

  swap(&x, &y);

  std::cout << "After swap: x = "
    << x << ", y = " << y << "\n";
}
Before swap: x = 5, y = 10
After swap: x = 10, y = 5

This technique uses the XOR operation to swap the values. Here's how it works:

  1. a = *a ^ *b: This XORs the values of a and b and stores the result in a.
  2. b = *a ^ *b: This XORs the new value of a with b, which gives us the original value of a, stored in b.
  3. a = *a ^ *b: This XORs the value in a with the new value in b, giving us the original value of b, now stored in a.

While this method is clever, it's worth noting that modern compilers are very good at optimizing standard swap operations. In most cases, using a temporary variable or std::swap() will be just as efficient and much more readable:

#include <iostream>
#include <utility>

int main() {
  int x{5};
  int y{10};

  std::cout << "Before swap: x = "
    << x << ", y = " << y << "\n";

  std::swap(x, y);

  std::cout << "After swap: x = "
    << x << ", y = " << y << "\n";
}

Remember, code readability is often more important than micro-optimizations. Use the pointer-based swap only when you have a specific reason to do so and when you're sure it provides a significant performance benefit in your specific use case.

Pointers

This lesson provides a thorough introduction to pointers in C++, covering their definition, usage, and the distinction between pointers and references

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Preventing Memory Leaks with Pointers
What are the best practices for avoiding memory leaks when working with pointers?
Smart Pointers vs Raw Pointers
What's the difference between smart pointers and raw pointers, and when should I use each?
Performance: Pointers vs References
What are the performance implications of using pointers vs. references in C++?
Pointers in Multithreaded Code
What are some common pitfalls when working with pointers in multithreaded applications?
Pointer Ownership in Complex Hierarchies
What are some strategies for managing pointer ownership in complex object hierarchies?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant