C++ Module 2 – Types, values and expressions
std::string and std::string_view
Build, change and search text with std::string, pass it cheaply as std::string_view, and catch the views that outlive their text before they print garbage.
What you will learn
- Create, modify and search strings
- Convert bytes to unsigned char before passing them to the <cctype> functions
- Pass read-only text efficiently with std::string_view
- Predict which strings are stored inside the object and which allocate
- Avoid string_views that outlive the text they point to
Before you start
What you’ll learn · Before you start
What you will learn
- Create, modify and search strings
- Convert bytes to unsigned char before passing them to the <cctype> functions
- Pass read-only text efficiently with std::string_view
- Predict which strings are stored inside the object and which allocate
- Avoid string_views that outlive the text they point to
Before you start
On this page
Splitting one line into 3 fields took 4 heap allocations as std::string and 1 as std::string_view in a GCC
build. std::string owns a sequence of characters: it allocates the memory, grows it as you append, and frees it
when the string is destroyed, so it is the type for text you build, change or keep. std::string_view (C++17)
owns nothing: it is a pointer and a length that refer to characters someone else owns, so making one, or taking a
piece of one, copies nothing.
That makes std::string_view the usual type for a function parameter that only reads text: it accepts a
std::string, a string literal or a part of either without a copy. The price is lifetime. A view that outlives its
text points at freed memory, and the compiler usually says nothing. Below, on Apple clang 21 and GCC 16, the savings
are measured, each library’s short-string storage recorded and the dangling view: A std::string_view, or another view that owns nothing, whose characters have been destroyed or freed. Reading it is undefined behaviour, which AddressSanitizer reports as a heap-use-after-free when the characters were on the heap.
reproduced under sanitizer: A compiler option, such as -fsanitize=address or -fsanitize=undefined, that builds extra checks into a program, so that memory errors and undefined behaviour are reported, with their source line, while it runs. More in “Finding memory bugs with AddressSanitizer and UBSan”.
A std::string owns its characters and changes them in place
A std::string behaves like a value: copying it copies the characters, and comparing two strings compares their
characters. The common operations, one per line:
// Building and changing a std::string: the string owns its characters and grows as needed.
#include <print>
#include <stdexcept>
#include <string>
int main() {
std::string order = "masala dosa";
order += ", filter coffee"; // append
order.insert(0, "2 x "); // insert at a position
std::println("{} ({} characters)", order, order.size());
order.replace(0, 1, "3"); // replace 1 character at position 0
order.erase(order.find(',')); // erase from the comma to the end
std::println("{}", order);
const std::string item = order.substr(4, 6); // a new string: 6 characters from position 4
std::println("item = {}, first letter = {}, last letter = {}", item, item[0], item.back());
try {
std::println("{}", item.at(10)); // at() checks the position; item[10] would not
} catch (const std::out_of_range&) {
std::println("at(10) threw std::out_of_range");
}
const bool apple_first = std::string("apple") < std::string("banana");
std::println("{}", apple_first ? "apple sorts first" : "banana sorts first");
} from session import run
run("""
clang++ --version | head -n 1
clang++ -std=c++23 -Wall -Wextra editing.cpp -o editing && ./editing
g++-16 --version | head -n 1
g++-16 -std=c++23 -Wall -Wextra editing.cpp -o editing && ./editing
""") Output
$ clang++ --version | head -n 1 Apple clang version 21.0.0 (clang-2100.3.34.2) $ clang++ -std=c++23 -Wall -Wextra editing.cpp -o editing && ./editing 2 x masala dosa, filter coffee (30 characters) 3 x masala dosa item = masala, first letter = m, last letter = a at(10) threw std::out_of_range apple sorts first $ g++-16 --version | head -n 1 g++-16 (Homebrew GCC 16.2.0) 16.2.0 $ g++-16 -std=c++23 -Wall -Wextra editing.cpp -o editing && ./editing 2 x masala dosa, filter coffee (30 characters) 3 x masala dosa item = masala, first letter = m, last letter = a at(10) threw std::out_of_range apple sorts first
Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 editing_session.py
+=appends,insert(pos, text)inserts before positionpos,replace(pos, count, text)replacescountcharacters, anderase(pos)removes everything fromposto the end.findreturns the position of the first match (here the comma), which is howeraseknew where to start.substr(pos, count)returns a new string with a copy of those characters.s[i]does not checki: a position past the end is undefined behaviour: What a program does when it breaks a rule for which the C++ standard sets no requirements, such as reading past the end of an array. The compiler may assume it never happens, so the result can change with the compiler and the optimisation level. More in “Object lifetime and undefined behaviour in C++”.s.at(i)checks and throwsstd::out_of_range, as the output shows.front()andback()give the first and last character.<,==and the other comparisons compare character by character, so"apple"sorts before"banana".
The program also shows that size() counts characters of type char, 30 for the order; the next lesson explains why
that is a count of bytes, not of letters, once text is not plain English.
Step through order from its first value through the five calls on it, and watch the characters it holds after each
one:
The drawing shows the whole scene; its 6 steps are listed below.
Every step as text
- The first value. order starts as masala dosa, 11 characters that the std::string owns: it allocated room for them, and it frees that room when it is destroyed.
- Appending. += adds ", filter coffee" at the end. The string makes room by itself whenever the characters no longer fit in what it holds.
- Inserting. insert(0, "2 x ") puts four characters before position 0, so every character after them moves along. The program printed the order with its size, 30 characters.
- Replacing. replace(0, 1, "3") takes the 1 character at position 0, the 2, and puts 3 in its place.
- Erasing. find(',') gives the position of the comma, 15, and erase(15) removes everything from there to the end. The program printed 3 x masala dosa.
- A new string. substr(4, 6) copies the 6 characters from position 4 into a new std::string: item = masala. Changing order now would leave item as it is.
Only the last call made a second string. Every other call changed order itself, which is what owning its
characters allows.
Key idea
A std::string owns its characters, so it can grow, shrink and change them in place; of the calls above, only
substr makes a new string.
Checkpoint
Check: A std::string owns and edits its text
The <cctype> functions need unsigned char values
Real input arrives with stray spaces and mixed case. normalise trims both ends, turns ASCII letters into lower case
and collapses each run of spaces and tabs into one space:
// A name normaliser: trims the ends, lower-cases ASCII letters
// and collapses runs of spaces into one.
#include <cctype>
#include <print>
#include <string>
#include <string_view>
std::string normalise(std::string_view text) {
std::string out;
out.reserve(text.size());
bool pending_space = false;
for (char c : text) {
// <cctype> functions need an unsigned char value:
// passing a negative char is undefined behaviour.
const auto u = static_cast<unsigned char>(c);
if (std::isspace(u)) {
pending_space = !out.empty(); // remember a gap, but never at the start
continue;
}
if (pending_space) out += ' ';
pending_space = false;
out += static_cast<char>(std::tolower(u));
}
return out;
}
int main() {
for (std::string_view raw : {" Aditi RAO ", "MEERA\tnair", "x", " "}) {
std::println("[{}] -> [{}]", raw, normalise(raw));
}
} from session import run
run("""
clang++ -std=c++23 -Wall -Wextra normalise.cpp -o normalise && ./normalise
g++-16 -std=c++23 -Wall -Wextra normalise.cpp -o normalise && ./normalise
""") Output
$ clang++ -std=c++23 -Wall -Wextra normalise.cpp -o normalise && ./normalise [ Aditi RAO ] -> [aditi rao] [MEERA nair] -> [meera nair] [x] -> [x] [ ] -> [] $ g++-16 -std=c++23 -Wall -Wextra normalise.cpp -o normalise && ./normalise [ Aditi RAO ] -> [aditi rao] [MEERA nair] -> [meera nair] [x] -> [x] [ ] -> []
Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 normalise_session.py
std::isspace and std::tolower come from C, and they take their argument as an int that must be a value of
unsigned char (or EOF); anything else is undefined behaviour, says section 7.4 of the C standard. Every byte of a
non-ASCII UTF-8 letter is above 127, and where char is signed, as it is on this Mac, such a byte is stored as a
negative char, so the code converts each char to unsigned char first. out.reserve(text.size()) allocates once,
since the result is never longer than the input.
This program prints the two bytes of é both ways:
// The two bytes of é in UTF-8, read as a plain char and as an unsigned char.
// Plain char is signed on this Mac, so a byte above 127 becomes a negative number.
#include <print>
#include <string_view>
int main() {
const std::string_view e_acute = "é"; // é: the bytes C3 A9
for (const char c : e_acute) {
const auto u = static_cast<unsigned char>(c);
std::println("byte {:02X}: as char {}, as unsigned char {}", u, static_cast<int>(c), static_cast<int>(u));
}
} Output
byte C3: as char -61, as unsigned char 195 byte A9: as char -87, as unsigned char 169
Recorded with Apple clang version 21.0.0 (clang-2100.3.34.2) on macOS 26 arm64. To run it yourself: clang++ -std=c++23 -O2 -Wall -Wextra -Werror -pedantic -fsanitize=address,undefined signed_bytes.cpp -o signed_bytes && ./signed_bytes
As a char, the bytes C3 and A9 are -61 and -87, values std::isspace is not defined for; as unsigned char they
are 195 and 169, which it is. Both compilers printed the same, because plain char is signed in both builds.
Passing a char straight to a <cctype> function
std::tolower(c) with a char from UTF-8 text passes -61 for the first byte of é. Write
std::tolower(static_cast<unsigned char>(c)), as normalise does, so every byte reaches the function as a value
from 0 to 255.
Checkpoint
Check: <cctype> and unsigned char
find gives a position; the newer searches give true or false
find returns the position of the first match, rfind of the last, and both return std::string::npos, the
largest value of std::size_t, when there is no match. C++20 and C++23 added three functions that answer the usual
yes-or-no questions directly:
// Searching a string: find gives a position or npos;
// contains, starts_with and ends_with give a bool.
#include <print>
#include <string>
int main() {
const std::string file = "report-q3-final.pdf";
const auto dash = file.find('-');
const auto zip = file.find(".zip");
std::println("first '-' at {}, last '-' at {}", dash, file.rfind('-'));
std::println("\".zip\" {}", zip == std::string::npos ? "not found (npos)" : "found");
std::println("contains(\"final\"): {}", file.contains("final")); // C++23
std::println("starts_with(\"report\"): {}", file.starts_with("report")); // C++20
// C++20, and case matters
std::println("ends_with(\".PDF\"): {}", file.ends_with(".PDF"));
} from session import run
run("""
clang++ -std=c++23 -Wall -Wextra search.cpp -o search && ./search
g++-16 -std=c++23 -Wall -Wextra search.cpp -o search && ./search
""") Output
$ clang++ -std=c++23 -Wall -Wextra search.cpp -o search && ./search
first '-' at 6, last '-' at 9
".zip" not found (npos)
contains("final"): true
starts_with("report"): true
ends_with(".PDF"): false
$ g++-16 -std=c++23 -Wall -Wextra search.cpp -o search && ./search
first '-' at 6, last '-' at 9
".zip" not found (npos)
contains("final"): true
starts_with("report"): true
ends_with(".PDF"): false
Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 search_session.py
contains (C++23) replaces s.find(x) != std::string::npos, and starts_with and ends_with (C++20) replace
comparisons with substr. All of them compare exactly, so ".PDF" does not match ".pdf".
The calls on "report-q3-final.pdf" and what each one gave back:
| Call | Result | Type of the result |
|---|---|---|
file.find('-') |
6 | std::size_t |
file.rfind('-') |
9 | std::size_t |
file.find(".zip") |
std::string::npos |
std::size_t |
file.contains("final") |
true |
bool |
file.starts_with("report") |
true |
bool |
file.ends_with(".PDF") |
false |
bool |
A position has to be compared with npos before it can be used; a bool answers the question as it stands, which is
why the three newer functions read more plainly when a yes or a no is all you need.
Checkpoint
Check: find, contains, starts_with, ends_with
A view is a pointer and a length, so it copies nothing
A std::string_view holds two things: a pointer to the first character and a length. A function that takes one can
be called with a string literal, a std::string or a view of part of either, and nothing is copied. substr on a
view makes another view, so splitting a line into fields allocates nothing. The program below proves it by counting
every heap allocation, through its own operator new:
// std::string_view refers to characters that someone else owns:
// making a view, or a piece of one, copies nothing.
// This program counts heap allocations to show it.
#include <cstdlib>
#include <new>
#include <print>
#include <string>
#include <string_view>
#include <vector>
static int allocations = 0;
// Every new and delete in the program goes through these two functions, so they can count.
void* operator new(std::size_t size) {
++allocations;
if (void* p = std::malloc(size == 0 ? 1 : size)) return p;
throw std::bad_alloc{};
}
void operator delete(void* p) noexcept { std::free(p); }
void operator delete(void* p, std::size_t) noexcept { std::free(p); }
// The same splitter twice: the first returns copies, the second views into the original text.
std::vector<std::string> split_copies(const std::string& text, char sep) {
std::vector<std::string> out;
out.reserve(8);
for (std::size_t start = 0;;) {
const auto end = text.find(sep, start);
out.push_back(text.substr(start, end - start)); // substr allocates when the piece is long
if (end == std::string::npos) return out;
start = end + 1;
}
}
std::vector<std::string_view> split_views(std::string_view text, char sep) {
std::vector<std::string_view> out;
out.reserve(8);
for (std::size_t start = 0;;) {
const auto end = text.find(sep, start);
out.push_back(text.substr(start, end - start)); // a string_view's substr never allocates
if (end == std::string_view::npos) return out;
start = end + 1;
}
}
int main() {
const std::string line = "Mumbai Central to New Delhi,Rajdhani Express number twelve,departs in the evening";
allocations = 0;
const auto copies = split_copies(line, ',');
std::println("{} fields as std::string: allocations = {}", copies.size(), allocations);
allocations = 0;
const auto views = split_views(line, ',');
std::println("{} fields as std::string_view: allocations = {}", views.size(), allocations);
std::println("first field: {}", views[0]);
} from session import run
run("""
clang++ -std=c++23 -Wall -Wextra views.cpp -o views && ./views
g++-16 -std=c++23 -Wall -Wextra views.cpp -o views && ./views
""") Output
$ clang++ -std=c++23 -Wall -Wextra views.cpp -o views && ./views 3 fields as std::string: allocations = 3 3 fields as std::string_view: allocations = 1 first field: Mumbai Central to New Delhi $ g++-16 -std=c++23 -Wall -Wextra views.cpp -o views && ./views 3 fields as std::string: allocations = 4 3 fields as std::string_view: allocations = 1 first field: Mumbai Central to New Delhi
Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 views_session.py
In both runs one allocation is the vector’s own storage (reserve(8)). With std::string, each field that is too
long for the string’s inline buffer allocated too: two with libc++ (Apple clang) and three with libstdc++ (GCC),
because “departs in the evening” has 22 characters, which fit inline in libc++ but not in libstdc++. With
std::string_view, the fields cost nothing. The C++ Core Guidelines give the same advice (rule SL.str.2): refer to
characters you do not own with std::string_view.
Step through both splits of the line and see where each of the three fields lives:
The drawing shows the whole scene; its 5 steps are listed below.
Every step as text
- Copies of the first two. split_copies calls substr on a std::string, which copies each field into a new std::string. 27 and 30 characters are more than either inline buffer holds, so both fields allocate, on both libraries.
- The third copy. The last field has 22 characters. That fits inside a libc++ string, which holds up to 22, but not a libstdc++ one, which holds up to 15, so only the GCC build allocates for it.
- Counting the copies. With the vector's own storage, that is 3 allocations with libc++ (Apple clang) and 4 with libstdc++ (GCC), as the two runs printed.
- Views instead. split_views calls substr on a std::string_view, which makes another view: a pointer into line and a length. No character is copied; field 1 is line from position 0, 27 characters long.
- One allocation. Only the vector allocated: allocations = 1 on both libraries, and the first view printed Mumbai Central to New Delhi. The views are valid only while line is alive.
Each view is two numbers into line. The diagram shows the same picture for one view, word, into another text,
and what is left of the view once that text is destroyed:
A std::string owns its characters; a std::string_view only points at them
Text description of the diagram
The diagram has four boxes.
- At the top right is std::string text, which holds a pointer, its size, 37, and its capacity. An arrow labelled owns goes from it to the characters.
- At the top left is std::string_view word, which holds only a pointer and a size, 8. An arrow labelled points into goes from it to the same characters.
- In the middle are the characters on the heap: "Namaste, and welcome to the C++ track". The string allocated them and frees them in its destructor.
- A dashed arrow leads down to the last box: once text is destroyed, word still points at the same place, which is now freed memory.
A view is not a string, and three differences matter:
- It is read-only: there is no
+=,insertorerase. - It need not end with a
'\0'.view.data()points into someone else’s characters, so passing it to a C function that expects a null-terminated string: A sequence of characters followed by a zero byte, '\0', which is how C functions such as std::puts and std::fopen find its end. std::string::c_str() always gives one; std::string_view::data() need not. can read past the end; the standard says so in a note todata(). Convert withstd::string(view)when you need a terminated copy. - It does not keep the characters alive. That is the section after the next one.
Key idea
A view is a pointer and a length into someone else’s characters: making one, or cutting a piece of one, costs nothing, and it can read them but never change or keep them.
Checkpoint
Check: A view is a pointer and a length
Small strings live inside the object
The standard does not say how a std::string stores its characters, but both libraries keep short strings inside
the std::string object itself, which avoids an allocation; longer strings go on the heap. This is the
small-string optimisation: Storing the characters of a short string inside the std::string object itself, so that no heap memory is allocated. The limit is the library's own: 22 characters with libc++ and 15 with libstdc++ in this track's 64-bit builds., and its limit differs:
// Short strings are stored inside the std::string object itself; longer ones on the heap.
// How short is short depends on the standard library.
#include <cstdint>
#include <print>
#include <string>
// Whether the characters of s sit inside the bytes of the object s.
bool stored_inside(const std::string& s) {
const auto object = reinterpret_cast<std::uintptr_t>(&s);
const auto characters = reinterpret_cast<std::uintptr_t>(s.data());
return characters >= object && characters < object + sizeof s;
}
int main() {
std::println("sizeof(std::string) = {}, capacity of an empty string = {}", sizeof(std::string),
std::string{}.capacity());
for (std::size_t length : {15, 16, 22, 23}) {
const std::string s(length, 'x');
std::println("{} characters: {}", length, stored_inside(s) ? "inside the object" : "on the heap");
}
} from session import run
run("""
clang++ -std=c++23 -Wall -Wextra sso.cpp -o sso && ./sso
g++-16 -std=c++23 -Wall -Wextra sso.cpp -o sso && ./sso
""") Output
$ clang++ -std=c++23 -Wall -Wextra sso.cpp -o sso && ./sso sizeof(std::string) = 24, capacity of an empty string = 22 15 characters: inside the object 16 characters: inside the object 22 characters: inside the object 23 characters: on the heap $ g++-16 -std=c++23 -Wall -Wextra sso.cpp -o sso && ./sso sizeof(std::string) = 32, capacity of an empty string = 15 15 characters: inside the object 16 characters: on the heap 22 characters: on the heap 23 characters: on the heap
Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 sso_session.py
| Standard library | sizeof(std::string) |
Characters stored inside |
|---|---|---|
| libc++ (Apple clang 21) | 24 bytes | up to 22 |
| libstdc++ (GCC 16) | 32 bytes | up to 15 |
Both are 64-bit builds on the same Mac. This explains the different allocation counts above, and it matters for the next example: a dangling view into a short string may still find the old characters in place, so a test with short text can hide the bug, while text longer than the inline buffer lives in heap memory that AddressSanitizer watches.
Checkpoint
Check: Short strings inside the object
A view that outlives its string reads freed memory
Here are the two classic ways to create a dangling view. greeting() returns a view into its local string, which
is destroyed when the function returns, and temporary.cpp keeps a view of a
temporary object: An unnamed object made while an expression is evaluated, such as the std::string that city + ' Central' produces. It is destroyed at the end of the full expression that made it, unless a reference bound to it keeps it alive. string, which is destroyed at the end of the statement that made it:
dangling.cpp
// A std::string_view must not outlive the string it looks at. Both mistakes below compile.
#include <print>
#include <string>
#include <string_view>
std::string_view first_word(std::string_view text) {
return text.substr(0, text.find(' ')); // fine: a view into the caller's text
}
std::string_view greeting() {
// longer than the small-string buffer
const std::string local = "Namaste, and welcome to the C++ track";
return first_word(local); // a view into local, which is destroyed when greeting() returns
}
int main() {
const std::string kept = "Vanakkam everyone";
std::println("{}", first_word(kept)); // fine: kept is still alive
const std::string_view word = greeting();
std::println("{}", word); // reads the memory of a destroyed string
} temporary.cpp
// A view of a temporary string: the temporary is destroyed at the end of the line that made it.
#include <print>
#include <string>
#include <string_view>
int main() {
const std::string city = "Thiruvananthapuram";
std::string_view shout = city + " Central"; // city + " Central" is a temporary std::string
std::println("{}", shout);
} from session import run
run("""
clang++ -std=c++23 -Wall -Wextra -g -fsanitize=address,undefined dangling.cpp -o dangling && ./dangling
g++-16 -std=c++23 -Wall -Wextra -g -fsanitize=address,undefined dangling.cpp -o dangling && ./dangling
clang++ -std=c++23 -Wall -Wextra -g -fsanitize=address,undefined temporary.cpp -o temporary && ./temporary
g++-16 -std=c++23 -Wall -Wextra -g -fsanitize=address,undefined temporary.cpp -o temporary && ./temporary
""") Output
$ clang++ -std=c++23 -Wall -Wextra -g -fsanitize=address,undefined dangling.cpp -o dangling && ./dangling
Vanakkam
=================================================================
==…==ERROR: AddressSanitizer: heap-use-after-free on address 0x…
READ of size 8 at 0x… thread T0
#28 in main dangling.cpp:21
0x… is located 0 bytes inside of 40-byte region [0x…,0x…)
freed by thread T0 here:
#2 in greeting() dangling.cpp:14
#3 in main dangling.cpp:20
previously allocated by thread T0 here:
#4 in greeting() dangling.cpp:12
#5 in main dangling.cpp:20
SUMMARY: AddressSanitizer: heap-use-after-free
[73 more lines of the report left out]
[exit status 134]
$ g++-16 -std=c++23 -Wall -Wextra -g -fsanitize=address,undefined dangling.cpp -o dangling && ./dangling
Vanakkam
=================================================================
==…==ERROR: AddressSanitizer: heap-use-after-free on address 0x…
READ of size 8 at 0x… thread T0
#8 in main dangling.cpp:21
0x… is located 0 bytes inside of 38-byte region [0x…,0x…)
freed by thread T0 here:
#5 in greeting() dangling.cpp:14
#6 in main dangling.cpp:20
previously allocated by thread T0 here:
#6 in greeting() dangling.cpp:12
#7 in main dangling.cpp:20
SUMMARY: AddressSanitizer: heap-use-after-free
[58 more lines of the report left out]
[exit status 134]
$ clang++ -std=c++23 -Wall -Wextra -g -fsanitize=address,undefined temporary.cpp -o temporary && ./temporary
temporary.cpp:8:30: warning: object backing the pointer will be destroyed at the end of the full-expression [-Wdangling-gsl]
8 | std::string_view shout = city + " Central"; // city + " Central" is a temporary std::string
| ^~~~~~~~~~~~~~~~~
1 warning generated.
=================================================================
==…==ERROR: AddressSanitizer: heap-use-after-free on address 0x…
READ of size 26 at 0x… thread T0
#28 in main temporary.cpp:9
0x… is located 0 bytes inside of 32-byte region [0x…,0x…)
freed by thread T0 here:
#2 in main temporary.cpp:8
previously allocated by thread T0 here:
#12 in main temporary.cpp:8
SUMMARY: AddressSanitizer: heap-use-after-free
[81 more lines of the report left out]
[exit status 134]
$ g++-16 -std=c++23 -Wall -Wextra -g -fsanitize=address,undefined temporary.cpp -o temporary && ./temporary
=================================================================
==…==ERROR: AddressSanitizer: heap-use-after-free on address 0x…
READ of size 26 at 0x… thread T0
#8 in main temporary.cpp:9
0x… is located 0 bytes inside of 31-byte region [0x…,0x…)
freed by thread T0 here:
#5 in main temporary.cpp:8
previously allocated by thread T0 here:
#7 in main temporary.cpp:8
SUMMARY: AddressSanitizer: heap-use-after-free
[59 more lines of the report left out]
[exit status 134]
Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 dangling_session.py
AddressSanitizer stops all four runs with a heap-use-after-free report that names the line that read the memory, the line that freed it and the line that allocated it (the sessions keep those lines of each report and write the addresses, which change on every run, as 0x…). The compilers are less helpful:
- Clang warns about the temporary (
-Wdangling-gsl, on by default); GCC 16 says nothing. - Neither compiler warns about the view returned from
greeting(), because it passes throughfirst_word, a function that is fine on its own. Clang does warn (-Wreturn-stack-address) when a function returns its local string directly as a view,return local;, but one function call in between is enough to hide it.
Step through dangling.cpp in time order and watch where word points when line 21 reads it:
The drawing shows the whole scene; its 6 steps are listed below.
Every step as text
- The call. After printing Vanakkam from a string that is still alive, main calls greeting() on line 20 to set word.
- local allocates. local holds "Namaste, and welcome to the C++ track", 37 characters, too many for either inline buffer, so line 12 allocates a block for them: 40 bytes with libc++, 38 with libstdc++.
- A view of the first word. first_word(local) returns text.substr(0, text.find(' ')): a view of Namaste, with the comma, a pointer into the heap block and the size 8. first_word itself is correct.
- The view goes back. greeting() returns that view to main, which stores it in word. A view copies a pointer and a length, never the characters.
- local is destroyed. As greeting() returns, its local string is destroyed, and its destructor frees the heap block: line 14 in the report. word still points at the first byte of it.
- Reading freed memory. Line 21 prints word, a read of 8 bytes in the freed block. AddressSanitizer stopped both builds there with a heap-use-after-free that names lines 21, 14 and 12, while neither compiler had warned.
No single line is wrong: first_word is correct, and so is greeting until it returns. The bug is that word lives
longer than local.
So the rules are about lifetimes, not syntax. Return a std::string from a function that builds text; take a
std::string_view parameter only for text you read during the call; store a std::string in a variable or a member
that must outlive the call; and run tests with -fsanitize=address, using text longer than the inline buffer.
Checkpoint
Check: Views that outlive their text
Choose the type by who owns the text and how long it must live
Every rule of this lesson comes down to two questions: who owns the characters, and how long must they stay usable? Answer them for a parameter, a variable or a member of your own, and the table rules out the types that would break, with the program above that showed each failure:
Answer the questions above: the table then marks what fits your case and says why the rest does not.
| Type | A literal costs nothing | A piece costs nothing | Owns its characters | Ends with a \0 for C |
|---|---|---|---|---|
| std::string_view | Good f("a literal") builds no std::string at all | Good substr on a view copies nothing: 3 fields, 1 allocation in all | Poor no: word read freed memory once local was destroyed | Poor not promised: data() may run past the last character |
| const std::string& | Poor a literal first becomes a temporary std::string | Poor a piece must be copied into a std::string first | Poor no: it refers to a string that someone else keeps alive | Good yes: c_str() always ends with \0 |
| std::string | Mixed copies the literal, and allocates when it is long | Mixed substr copies: 2 fields allocated with libc++, 3 with libstdc++ | Good yes: it frees its characters only in its own destructor | Good yes: c_str() always ends with \0 |
How the answers decide
- “Must the text stay usable after the call, as a member, a return value, a container element or text a function keeps?” Yes, it is kept: rules out std::string_view. A view owns nothing: the one greeting() returned read freed memory once local was destroyed. Whatever keeps the text must own it, as a std::string does.
- “Must the text stay usable after the call, as a member, a return value, a container element or text a function keeps?” Yes, it is kept: adds a caveat to const std::string&. const std::string& owns nothing either: it suits the parameter of a function that copies the text into a std::string it keeps, never the thing that is kept.
- “Does the text go to a C function that needs a terminating \0?” Yes, a C function reads it: rules out std::string_view. A view need not end with \0, so a C function reading view.data() can run past its end; std::string(view) makes a terminated copy.
- “Must the text stay usable after the call, as a member, a return value, a container element or text a function keeps?” No, it is read during the call and “Is it made from string literals or from pieces of a longer text?” Yes, literals or pieces: adds a caveat to const std::string& and std::string. Each literal or piece is first copied into a std::string, which allocates when the text is longer than the inline buffer: 22 characters with libc++, 15 with libstdc++.
- “Must the text stay usable after the call, as a member, a return value, a container element or text a function keeps?” No, it is read during the call and “Does the text go to a C function that needs a terminating \0?” No: Text that is only read while its string exists is what std::string_view is for: literals, strings and pieces of either, with nothing copied.
In short: std::string_view for text a function reads during the call; std::string for text that is built, kept
or must end with a zero for C; and const std::string& for a string the caller already has, when the function needs
c_str() for C or copies the text into something it keeps.
Key idea
Own the text wherever it must outlive its source, and view it wherever it is only read: a view costs nothing until it outlives what it points at.
Checkpoint
Check: Choosing a type for text
Practise: split and join
Write split, which cuts a std::string_view at every separator and returns the pieces as views into the original
text (keeping empty pieces), and join, which glues pieces together with a separator between them. The checks in
main must all print ok; the last one checks that split copies nothing.
// Exercise: write split and join. Build and run this file: every check must say "ok".
// split("a,b,c", ',') gives the views {"a", "b", "c"}; empty fields are kept.
// join({"a", "b", "c"}, ", ") gives the string "a, b, c".
#include <print>
#include <string>
#include <string_view>
#include <vector>
std::vector<std::string_view> split(std::string_view text, char separator) {
(void)text;
(void)separator;
return {}; // replace this
}
std::string join(const std::vector<std::string_view>& parts, std::string_view separator) {
(void)parts;
(void)separator;
return {}; // replace this
}
int failures = 0;
void check(bool passed, std::string_view what) {
std::println("{} {}", passed ? "ok " : "FAILED", what);
if (!passed) ++failures;
}
int main() {
using Parts = std::vector<std::string_view>;
check(split("idli,dosa,vada", ',') == Parts{"idli", "dosa", "vada"}, "three fields");
check(split("chai", ',') == Parts{"chai"}, "no separator: one field");
check(split("a,,b", ',') == Parts{"a", "", "b"}, "an empty field in the middle is kept");
check(join({"idli", "dosa", "vada"}, ", ") == "idli, dosa, vada", "join with a separator");
check(join({"chai"}, " + ") == "chai", "one part: no separator");
const std::string line = "Pune;Nashik;Nagpur";
const auto parts = split(line, ';');
check(!parts.empty() && parts[0].data() == line.data(),
"the views point into the original text (no copies)");
std::println("{} of 6 checks failed", failures);
return failures == 0 ? 0 : 1;
} As given, every check fails:
from session import run
run("""
clang++ -std=c++23 -Wall -Wextra -fsanitize=address,undefined split_join.cpp -o split_join && ./split_join
""") Output
$ clang++ -std=c++23 -Wall -Wextra -fsanitize=address,undefined split_join.cpp -o split_join && ./split_join FAILED three fields FAILED no separator: one field FAILED an empty field in the middle is kept FAILED join with a separator FAILED one part: no separator FAILED the views point into the original text (no copies) 6 of 6 checks failed [exit status 1]
Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 split_join_session.py
First hint: loop with text.find(separator, start), and stop after the piece for which find returns npos.
text.substr(start, end - start) gives the right piece even then, because substr stops at the end of the view.
Once all six checks pass, test yourself on the seven ideas. The session begins with the idea you have answered least well, and every option you pick says what it gets right or misses:
Practice that adapts to you
Practise this lesson
17 questions about 7 ideas of this lesson. Each answer is explained at once, and what you get right is kept in this browser.
Interview questions
Fresher · When should a function take std::string_view instead of const std::string&?
When it only reads the text during the call. A std::string_view parameter accepts string literals, std::string
objects and parts of either without creating a temporary std::string, so a call such as f("literal") allocates
nothing, and taking a piece costs nothing. Take const std::string& (or a std::string by value) when the function
needs a terminated string for a C API, or keeps the text after it returns.
Interviewers then check that you know where the rule stops. Choose an answer to each follow-up below; every option explains what it gets right or misses:
Checkpoint
Interview round: two follow-up questions
Model answers open with any pass: ₹79 for 1 week ($2.99), one payment that never renews.
-
Mid to senior
A class stores a std::string_view member set from a constructor argument. Objects made from string literals work, but objects made from lines read in a loop print garbage later. Explain what happens and how you would fix the class.
-
Mid to senior
A log parser splits each 300-byte line into 12 fields stored as std::string, at 50,000 lines a second, and a profile shows most of the time in malloc. How would you reduce the cost, and what new risk do you take on?
Key takeaways
std::stringowns its characters;std::string_viewis a pointer and a length into characters owned elsewhere, sosubstron astd::stringcopies and on astd::string_viewcopies nothing, as the allocation counts show.at()checks positions,[]does not;contains(C++23),starts_withandends_with(C++20) replacefindcomparisons.size()countscharvalues, which are bytes; pass them tostd::isspace,std::tolowerand the other<cctype>functions asunsigned char. The next lesson is about what bytes mean for Unicode text.- Short strings live inside the object: up to 22 characters with libc++, 15 with libstdc++.
- A view must not outlive its text: never return a view of a local string or keep a view of a temporary; Clang warns about some cases, AddressSanitizer catches them at run time.
- Take
std::string_viewfor text you read during a call, and hold astd::stringfor text that is kept, built or passed to a C function.
Terms in this lesson
- dangling view
- A std::string_view, or another view that owns nothing, whose characters have been destroyed or freed. Reading it is undefined behaviour, which AddressSanitizer reports as a heap-use-after-free when the characters were on the heap.
- null-terminated string
- A sequence of characters followed by a zero byte, '\0', which is how C functions such as std::puts and std::fopen find its end. std::string::c_str() always gives one; std::string_view::data() need not.
- small-string optimisation
- Storing the characters of a short string inside the std::string object itself, so that no heap memory is allocated. The limit is the library's own: 22 characters with libc++ and 15 with libstdc++ in this track's 64-bit builds.
- temporary object
- An unnamed object made while an expression is evaluated, such as the std::string that city + ' Central' produces. It is destroyed at the end of the full expression that made it, unless a reference bound to it keeps it alive.
References
- Working Draft, Programming Languages: C++ (N4950), 23.3 String view classes, 23.4 String classes, 6.7.7 Temporary objects (ISO/IEC JTC1/SC22/WG21)
- P0254R2: Integrating std::string_view and std::string (ISO/IEC JTC1/SC22/WG21)
- P0457R2: String Prefix and Suffix Checking (ISO/IEC JTC1/SC22/WG21)
- P1679R3: string contains function (ISO/IEC JTC1/SC22/WG21)
- ISO/IEC 9899:2024 working draft (N3220), 7.4 Character handling <ctype.h> (ISO/IEC JTC1/SC22/WG14)
- C++ Core Guidelines: SL.str.2 Use std::string_view or gsl::span<char> to refer to character sequences (Standard C++ Foundation)
- AddressSanitizer (LLVM Project)
- Diagnostic flags in Clang: -Wdangling-gsl (LLVM Project)
Related tools
Report a problem with this lesson
Kept only in this browser. Your Learn progress