References and Pointers to Local C++ Variables

Explanation of automatic storage duration, and why it is important to consider when returning pointers and references from functions
This lesson is 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

3D art showing a female blacksmith character
Ryan McCombe
Ryan McCombe
Posted

Now that we’re working with pointers and references, we need to talk briefly about memory management.

This is a large topic - we have a full chapter dedicated to it in the intermediate course. For now, we just need to understand an important limitation with the way we’ve been creating variables so far.

Let's demonstrate that limitation with a seemingly simple program:

#include <iostream>
using namespace std;

// Note, this returns a pointer
int* GetNumber() {
  int Number { 1 };
  return& Number;
}

int main() {
  int* Result { GetNumber() };
  cout << &Result;
}

We might expect this to log out 1, but that is not the case. In fact, on my machine, it logged out 1654145864. The number is different every time I run the program.

Hopefully, the compiler would have given us a warning that we might have been doing something wrong.

Let's delve a little deeper into this warning, as it’s important that we understand what’s going on here.

C++ Warning: "Reference to stack memory associated with local variable returned"

In the introductory lessons, we covered the idea of the stack, and how our functions each create stack frames. Stack frames are a form of memory management.

The parameters and local variables that our functions create are stored within these stack frames.

Diagram showing the return of a pointer from a function

However, stack frames are an automated form of memory management. When the function ends, its stack frame, and all the memory it used, are released.

This means that if we have a pointer to a variable that is stored in a stack frame, that pointer is only useful as long as that stack frame exists.

Once the corresponding function has ended, the stack frame is gone, and the memory is freed up to be used for other purposes. That means we no longer know what is stored in the memory location our pointer is pointing at.

Diagram showing a dangling pointer

Why this only applies to references and pointers

In the past, we’ve been creating and returning local variables without issues. When we return something from a stack frame, that value gets moved or copied to the stack frame that called our function.

When the thing we returned was the value, everything works as expected.

However, when a pointer (or reference) is returned, that pointer can refer to a memory location that is about to be freed. When that pointer is then used, unpredictable things can happen, as the first example demonstrated.

C++ Storage Durations

The objects we create in C++ have a few different options for when their memory is released. The scenarios we're covering here - local variables in functions - are examples of what is commonly called automatic storage duration.

Avoiding Automatic Storage Duration Problems

In the next course, we cover memory in more detail. This includes techniques for how we can opt out of this form of automatic memory management when we create a variable.

For now, it’s important we’re aware of this behaviour. When we create variables, we want to create them in a place where they will last as long as they are needed.

If we need a variable to last the full duration of our program, places we can create it include:

  • The global scope
  • Inside the main function
  • Part of an object that was created in one of those locations. In other words, the variables stored in an instance of a class will survive as long as that instance survives

Was this lesson useful?

Ryan McCombe
Ryan McCombe
Posted
This lesson is 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

References and Pointers
3D art showing a progammer setting up a development environment
This lesson is 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

Free, unlimited access!

This course includes:

  • 66 Lessons
  • Over 200 Quiz Questions
  • Capstone Project
  • Regularly Updated
  • Help and FAQ
Next Lesson

C++ Polymorphism

A friendly introduction to polymorphism covering what it is, and why it is useful
3D art showing a programmer
Contact|Privacy Policy|Terms of Use
Copyright © 2023 - All Rights Reserved