Pointers and References: Never Return a Pointer to a Local Function Variable

What is it?

In C++, local function variables are stored on the stack. Their lifetime is bound to the function they are defined in. This means, once the function is done executing, the local variables are cleaned up, and their memory is reclaimed. Therefore, returning a pointer to a local variable from a function is considered bad practice because the variable's memory may no longer be valid when the pointer is used. This can lead to undefined behavior, which may include program crashes, data corruption, and other unpredictable outcomes.


Syntax

Here is a simple and incorrect way of returning a pointer to a local variable:

int* badPractice() {
    int localVariable = 10;
    return &localVariable;
}

Where,

  • badPractice: This is the function name.
  • localVariable: This is a local variable inside the function.
  • &localVariable: This returns the address of the local variable.



How to use it

You should avoid returning pointers to local variables from functions. Instead, consider alternatives like returning by value, returning a pointer to a dynamically allocated variable, or returning a data member of a class object.



Program code snippet example

Here's an example of how you might mistakenly return a pointer to a local variable, and a correct way to do this:

#include <iostream>

int* incorrectWay() {
    int localVariable = 10;
    return &localVariable;
}

int* correctWay() {
    int* localVariable = new int(10);
    return localVariable;
}

int main() {
    int* badPointer = incorrectWay();
    // Do not use badPointer here, it points to an invalid memory location!

    int* goodPointer = correctWay();
    std::cout << *goodPointer << std::endl; // This is safe.

    delete goodPointer; // Remember to clean up dynamically allocated memory!
    return 0;
}

Output:

10



Important to know

  • Always be careful when dealing with pointers in C++. Improper use of pointers can lead to a variety of problems, including memory leaks and undefined behavior.
  • The local variables of a function get destroyed after the function call is over. Therefore, a pointer to a local variable becomes a dangling pointer after the function call.
  • The new keyword is used to allocate memory dynamically on the heap. This memory stays valid until it's explicitly deallocated using delete.



Best practices

When working with pointers and functions in C++, keep these best practices in mind:

  1. Avoid returning pointers to local variables: As mentioned, this can lead to undefined behavior.

  2. Be mindful of memory ownership: If a function returns a pointer to a dynamically allocated variable, the caller becomes responsible for deallocating the memory. Failing to do so will result in a memory leak.

  3. Consider alternatives: If possible, consider alternatives to returning pointers. These might include returning objects by value or using smart pointers, which automatically manage memory and can help prevent memory leaks.




Delete the function call for or_this because it is returning the address to a local variable.