Previous section   Next section

Imperfect C++ Practical Solutions for Real-Life Programming
By Matthew Wilson
Table of Contents
Chapter 31.  Return Value Lifetime


31.1. A Taxonomy of Return Value Lifetime Gotchas

The issue of RVL comes about from the fact that not everything in C++ is a value type. Sometimes we refer to an entity by reference. This reference can, in C++, be either a pointer (e.g., X*) or a C++ reference (e.g., X&), but in either case it is effectively a simple value that denotes the location of the entity that it references.

The RVL problem comes about because it is possible for such a reference to become stale, by virtue of the fact that the entity that it references changes or ceases to exist. There are three main ways in which this problem can occur in C++, all of which will impact on our implementations for integer to string conversion.

31.1.1 Local Variables

Almost every C++ basic instructional or gotcha book [Dewh2003, Meye1998, Stro1997] contains a warning about returning the address of local variables, and I'm going to take it that you're aware of the dangers.[1] We'll refer to this as RVL-LV.

[1] If you're not at that level, then I certainly commend you on your grit in making it this far through this book.

31.1.2 Local Statics

As we've mentioned previously (see Chapter 11), the use of local statics can be either menace or godsend, depending on the circumstances in which they're used. In almost all circumstances in which a local static is involved in call-by-reference, it is a menace. We'll refer to this as RVL-LS.

31.1.3 Postdestruction Pointers

As we saw in section 16.2, and in the discussions of Conversion and Access Shims (see Chapter 20), it is all too easy in C++ to assign a pointer/reference to something that is held in an object instance and that is subsequently destroyed. We'll refer to this as RVL-PDP.


      Previous section   Next section