References

Dangers of Returning References to Local Objects

What are the implications of returning a reference from a function that creates a local object?

3D character art

Returning a reference to a local object from a function is a dangerous practice that can lead to undefined behavior. This is because local variables are destroyed when the function exits, leaving the returned reference dangling - it refers to memory that is no longer valid.

Let's look at an example to illustrate this problem:

#include <iostream>
#include <string>

std::string& CreateGreeting() {
  std::string greeting{"Hello, World!"};  
  return greeting;                        
}

int main() {
  std::string& ref = CreateGreeting();
  std::cout << "Greeting: " << ref << '\n';
}

This code might appear to work on some compilers or in some situations, but it's actually undefined behavior. Here's what's happening:

  1. CreateGreeting() creates a local std::string object.
  2. It returns a reference to this local object.
  3. The function exits, and the local greeting object is destroyed.
  4. The reference in main() now points to destroyed memory.
  5. Attempting to use this reference leads to undefined behavior.

Some compilers might warn about this issue:

warning: reference to local variable 'greeting' returned

The consequences of this undefined behavior can vary:

  • The program might seem to work correctly (dangerous, as it masks the problem).
  • It might crash.
  • It might produce unexpected output.
  • It might corrupt memory, leading to hard-to-debug issues elsewhere in the program.

To fix this, you have several options:

Return by value instead of by reference:

std::string CreateGreeting() {
  std::string greeting{"Hello, World!"};
  return greeting;
}

If you need to return a reference, ensure it's to a static or global object:

const std::string& CreateGreeting() {
  static const std::string greeting{
    "Hello, World!"
  };
  return greeting;
}

If the object needs to outlive the function but shouldn't be static, consider dynamic allocation (but be careful with memory management):

#include <iostream>

std::string* CreateGreeting() {
  return new std::string{"Hello, World!"};
}

int main() {
  std::string* pGreeting = CreateGreeting();
  std::cout << "Greeting: " << *pGreeting << '\n';
  // Don't forget to free the memory!
  delete pGreeting;
}
Greeting: Hello, World!

In modern C++, option 1 (return by value) is often the best choice. The compiler can usually optimize this to be as efficient as returning a reference, without the dangers of dangling references.

Remember, when working with references, always ensure that the lifetime of the referenced object extends beyond the lifetime of the reference itself. This is especially crucial when returning references from functions.

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