Pitfalls and Lifetimes
Avoid dangling views.
Pitfalls and Lifetimes is a free C++ Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Views Do Not Own
A string_view only points at characters owned elsewhere. If that storage disappears, the view dangles, accessing it is undefined behavior.
Dangling from a Temporary
Binding a view to a temporary std::string is dangerous: the temporary dies at the end of the statement, leaving the view pointing at freed memory.
#include <string>
#include <string_view>
std::string make() { return "temp"; }
int main() {
std::string_view sv = make(); // dangles immediately
// using sv here is undefined behavior
(void)sv;
}Returning a View to a Local
Never return a view that points into a local string; the local is destroyed when the function returns.
#include <string>
#include <string_view>
std::string_view bad() {
std::string local = "oops";
return local; // local dies, view dangles
}
int main() { (void)bad(); }Safe: View of a Living String
A view is fine as long as the source outlives it. Here the string lives for the whole function.
#include <iostream>
#include <string>
#include <string_view>
int main() {
std::string s = "alive";
std::string_view sv = s;
std::cout << sv << "\n"; // safe
}Not Null-Terminated
A view's data is not guaranteed to end in a \0. Passing sv.data() to a C function expecting a C-string can read past the end.
Use size(), Not strlen
Always rely on sv.size() for length rather than C-string functions, since a view may be a slice of a larger buffer.
#include <iostream>
#include <string_view>
int main() {
std::string_view sv = std::string_view("hello world").substr(0, 5);
std::cout << sv.size() << "\n"; // 5, not 11
}Modifying the Source
If the underlying string is reallocated (for example by push_back growing its capacity), existing views into it become invalid.
Storing Views in Members
Be cautious storing a string_view as a class member; you must guarantee the referenced data outlives the object. When in doubt, store a std::string instead.
Parameters Are Usually Safe
Using string_view as a function parameter is the safe, idiomatic case: the argument outlives the call.
#include <iostream>
#include <string_view>
size_t len(std::string_view sv) { return sv.size(); }
int main() {
std::cout << len("temporary is fine here") << "\n";
}Copy When You Must Persist
If you need to keep text beyond the lifetime of its source, copy it into a std::string. The copy is the price of ownership.
#include <string>
#include <string_view>
std::string keep(std::string_view sv) {
return std::string(sv); // owns a copy
}
int main() { (void)keep("persist"); }Rule of Thumb
Prefer string_view for parameters and short-lived locals. Avoid it as a return type or long-lived member unless the data's lifetime is clearly longer.
Quick Check
Check your understanding of view lifetimes.
Recap
You learned view pitfalls and lifetimes:
- Views do not own data; the source must outlive the view.
- Avoid views to temporaries and locals.
- Views are not guaranteed null-terminated, use
size(). - Copy into a
std::stringwhen you need to persist text.
Frequently asked questions
Is the “Pitfalls and Lifetimes” lesson free?
Yes — the full text of “Pitfalls and Lifetimes” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.
What will I learn in “Pitfalls and Lifetimes”?
Avoid dangling views. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Pitfalls and Lifetimes” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Why string_view
- Creating Views
- Pitfalls and Lifetimes
- String Algorithms