C++ Academy · Lesson

Pitfalls and Lifetimes

Avoid dangling views.

Lesson 3 of 413 steps

Pitfalls and Lifetimes is a free C++ Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.

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::string when you need to persist text.
Free to start

Learn C++ with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “Pitfalls and Lifetimes” lesson free?

Yes — any 3 lessons of the C++ Academy learning path, including “Pitfalls and Lifetimes”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

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

  1. Why string_view
  2. Creating Views
  3. Pitfalls and Lifetimes
  4. String Algorithms
← Back to C++ Academy