Returning a Reference (or Pointer) to a Local Object¶
0. Scope and Conclusions First¶
This article discusses one of the most common C++ lifetime bugs:
- returning a reference to an automatic local object,
- returning a pointer to an automatic local object,
- returning views/iterators that indirectly refer to automatic local objects.
Core conclusion:
- If a function returns, all of its automatic local objects are already destroyed.
- Any reference/pointer/view that still points to those objects is dangling.
- Reading or writing through a dangling handle is undefined behavior (UB).
Engineering conclusion:
- Prefer return-by-value.
- Return references only when the referenced object is guaranteed to outlive all uses.
- Make ownership and borrowing contracts explicit at API boundaries.
1. Minimal Failing Case¶
What is wrong:
shas automatic storage duration.- Its lifetime ends when
bad()exits. - The returned reference refers to a non-existent object.
The same bug shape exists for pointers:
And for indirect handles:
std::string_view bad_view() {
std::string s = "cpp";
return std::string_view{s}; // view to dead buffer
}
2. Why This Is UB: Storage Duration vs Object Lifetime¶
You must separate two concepts:
- Storage duration (where memory comes from and how long storage exists).
- Object lifetime (when an object begins and ends as a valid C++ object).
For automatic locals:
- storage is tied to function scope,
- lifetime ends when scope exits,
- destructor runs before function returns to caller.
So the caller never observes a valid local object through a returned reference.
In formal terms, once lifetime ends, any glvalue/pointer designating that object no longer denotes a valid object for access. Using it is UB.
3. “But It Seems to Work on My Machine”¶
This bug is often deceptive because:
- Stack memory may still contain old bytes briefly.
- Optimizations may reorder code.
- Different builds produce different symptoms.
Possible outcomes:
- apparently correct output,
- garbage values,
- intermittent crashes,
- silent data corruption.
All of these are consistent with UB.
If behavior looks stable in debug and fails in release, that is a classic UB signature.
4. Common Variants in Real Code¶
4.1 Returning reference to local container element¶
4.2 Returning C-string pointer from local std::string¶
const char* bad_cstr() {
std::string s = "hello";
return s.c_str(); // pointer invalid after return
}
4.3 Returning iterator into local container¶
std::vector<int>::const_iterator bad_it() {
std::vector<int> v{1, 2, 3};
return v.begin(); // iterator dangling
}
4.4 Returning std::span into local array¶
std::span<const int> bad_span() {
int a[] = {1, 2, 3};
return std::span<const int>(a); // span points to dead array
}
4.5 Returning lambda capturing local by reference¶
5. Correct Patterns¶
5.1 Return by value (default choice)¶
Why this is preferred:
- clear ownership transfer,
- no dangling borrow,
- modern compilers optimize copies aggressively.
5.2 Return reference to stable owner object¶
class Config {
public:
const std::string& name() const noexcept { return name_; }
private:
std::string name_ = "service";
};
This is safe because:
- reference points to
name_inside*this, - caller must ensure
Configobject remains alive.
5.3 Return smart pointer for shared ownership semantics¶
Use when ownership is intentionally shared.
5.4 Caller-supplied output object¶
Good for hot paths when you want explicit reuse of caller storage.
6. Lifetime-Safe API Design Rules¶
At API boundaries, make contracts explicit:
- Owned return: return value/smart pointer.
- Borrowed return: return reference/view only if source owner is explicit and stable.
- No hidden owner: avoid returning borrow tied to hidden temporaries.
Recommended naming/documentation style:
get_*_copy()for owned values,*_view()for borrowing views,- mention precondition: “returned view valid while
Xremains alive and unmodified”.
7. What About static Locals?¶
This is a distinct case:
const std::string& singleton_name() {
static const std::string s = "cpp";
return s; // lifetime: program duration
}
This avoids dangling because static local lifetime is program-long.
But evaluate trade-offs:
- hidden global state,
- initialization/destruction ordering concerns across translation units,
- potential testability costs.
Use intentionally, not as a blanket workaround.
8. Return-by-Value Cost Myth (Modern C++)¶
Many buggy APIs exist because developers overestimated copy cost.
In modern C++:
- copy elision is common,
- move construction is cheap for many standard/library types,
- value semantics greatly simplify correctness.
Correctness-first rule:
$$ ext{Safe lifetime contract} > \text{micro-optimization based on assumption} $$
If profiling later proves a hotspot, optimize with measured evidence.
9. Review Checklist (Code Review / PR)¶
When reviewing return statements, ask:
- Is return type a reference/pointer/view/iterator?
- Does it refer to automatic local state?
- Does it refer to temporary subobject/buffer?
- Is owner visible and guaranteed alive in caller context?
- Would return-by-value remove ambiguity?
Red flags:
return local;where function returnsT&orconst T&.return &local;.return local.c_str();.return std::string_view(local);.- returning lambda with
[&]capture from function.
10. Tooling and Diagnostics¶
Useful defenses:
- Compiler warnings:
- enable high warning levels,
- treat lifetime warnings as errors in CI.
- Sanitizers:
- AddressSanitizer can catch many dangling accesses.
- Static analysis:
- lifetime and escape analysis rules,
- custom checks for return of locals by reference/pointer.
No tool is complete; combine tools with strong API design.
11. Migration Strategy for Legacy Code¶
If legacy APIs already return borrowed references dangerously:
- Classify each function:
- always dangling,
- conditionally safe,
- safe by ownership contract.
- Introduce safe replacements:
- value-return overloads,
- explicit owner parameters.
- Deprecate unsafe signatures.
- Incrementally migrate call sites.
- Add regression tests under sanitizers.
Prefer staged migration over big-bang rewrites.
12. Bad vs Good Patterns (Compact Table)¶
| Pattern | Safety | Better Choice |
|---|---|---|
return T& to local T |
UB | return T |
return T* to local T |
UB | return T or owner pointer |
return string_view to local string |
UB | return string |
| return iterator to local container | UB | return container/value |
| return ref to member of stable object | potentially safe | document owner-lifetime contract |
13. Key Takeaways¶
- Local automatic objects die at function exit.
- Returning references/pointers/views to them creates dangling handles.
- Access through dangling handles is UB.
- Return-by-value is the default robust interface in modern C++.
- If borrowing is required, make owner and lifetime constraints explicit and verifiable.
Correctness of lifetime contracts is not optional API polish; it is a foundational safety property.
Example¶
A runnable version of this topic: