# Strings, string_view, and span

> How std::string owns characters with Small String Optimization while std::string_view and std::span borrow contiguous memory safely without hidden allocations.

- **Source:** C++ Atlas (https://cppatlas.com/topics/strings-views-and-spans)
- **Track:** Containers and strings
- **Estimated reading time:** 10 minutes
- **ISO C++ clauses:** [basic.string] (https://eel.is/c++draft/basic.string), [string.require] (https://eel.is/c++draft/string.require), [string.view] (https://eel.is/c++draft/string.view), [string.view.cons] (https://eel.is/c++draft/string.view.cons), [views.span] (https://eel.is/c++draft/views.span)
- **cppreference links:** [std::basic_string](https://en.cppreference.com/w/cpp/string/basic_string), [std::basic_string_view](https://en.cppreference.com/w/cpp/string/basic_string_view), [std::span](https://en.cppreference.com/w/cpp/container/span), [std::as_bytes / std::as_writable_bytes](https://en.cppreference.com/w/cpp/container/span/as_bytes), [std::dynamic_extent](https://en.cppreference.com/w/cpp/container/span/dynamic_extent)
- **Prerequisites:** containers-and-iterator-invalidation, reference-binding-and-temporaries

Taking `const std::string&` as a function parameter used to be reflexive C++ advice, yet it silently couples every caller to heap-capable ownership. Passing a string literal `"GET /health"` or slicing three bytes out of a network packet through `const std::string&` forces construction of a temporary `std::string`, copying characters and potentially touching the allocator.

Modern C++ separates **storage ownership** from **contiguous borrowing**. `std::string` (`std::basic_string<char>`) owns its character buffer, whereas C++17 `std::string_view` and C++20 `std::span<T>` are lightweight, non-owning descriptors—essentially a pointer paired with a length—passed by value across API boundaries.

> **Mental model.** Ownership flows upward in return values (`std::string`, `std::vector<T>`); non-owning views flow downward in parameters (`std::string_view`, `std::span<const T>`). A view borrows memory it did not allocate and must never outlive the owner behind it.

## std::string, SSO, and the null-terminator invariant

Since C++11, `[string.require]` mandates that `std::basic_string` stores its elements contiguously in memory and maintains a trailing null terminator `CharT()` at `s[s.size()]`. Unlike pre-C++11 reference-counted copy-on-write strings—which were outlawed by C++11 thread-safety and invalidation rules—every modern `std::string` owns a unique buffer, and C++17 made `s.data()` return a mutable `CharT*` for non-`const` strings.

To avoid heap allocations for short strings, all major standard libraries implement the **Small String Optimization (SSO)**. When a string fits inside the object's inline byte budget (typically 15 characters on libstdc++ with `sizeof(std::string) == 32`, or 22 characters on libc++ with `sizeof(std::string) == 24`), the characters live inside the `std::string` object itself rather than on the heap:

```text
Short string (SSO active): [ inline char buffer[N] | inline size | flag ]  (no heap alloc)
Long string  (heap mode):  [ char* ptr | size | capacity        | flag ] ──▶ [ heap chars + '\0' ]
```

SSO makes short strings fast, but creates a subtle invalidation rule: **moving a `std::string` does not necessarily preserve pointers or views into its characters**. Moving a heap-mode string steals the heap pointer, whereas moving an SSO string must copy the inline bytes into the destination object and leave the source decoupled.

> **Pitfall.** Because moving an SSO `std::string` copies inline bytes into the new object's stack footprint, any `std::string_view`, pointer, or iterator pointing into the moved-from string becomes dangling or refers to the old buffer rather than the moved-to string.

When interfacing with C APIs that fill a caller-provided buffer, C++23 adds `resize_and_overwrite(max_cap, op)` so you can write directly into `std::string` storage without pre-zeroing unused bytes:

```cpp
#include <algorithm>
#include <cstring>
#include <string>

std::string build_greeting() {
    std::string out;
    out.resize_and_overwrite(64, [](char* buf, std::size_t cap) noexcept {
        constexpr char msg[] = "lucid-cpp";
        const std::size_t len = std::min(cap, sizeof(msg) - 1);
        std::memcpy(buf, msg, len);
        return len; // std::string writes buf[len] = '\0' automatically
    });
    return out;
}
```

## std::string_view: non-owning text without guarantees

`std::string_view` (`std::basic_string_view<char>`) consists solely of `{const char* data, std::size_t size}`. It is trivially copyable, two machine words wide, and converts implicitly from string literals, `const char*`, `std::string`, and (since C++20) contiguous sized character ranges. Because copying it copies two registers, pass `std::string_view` **by value**, never by `const std::string_view&`.

Two invariants separate `std::string_view` from `std::string`:

1. **No null-termination guarantee.** Slicing with `sv.remove_suffix(3)` or `sv.substr(2, 4)` simply adjusts `size_` or `data_` without copying bytes or inserting `'\0'`. Passing `sv.data()` directly to C functions expecting null-terminated strings (`std::fopen`, `std::printf("%s", ...)`, `std::strlen`) reads past the slice end and invokes undefined behavior.
2. **No lifetime ownership.** Binding `std::string_view` to a temporary `std::string` leaves the view dangling as soon as the temporary dies at the end of the full-expression. Returning a `std::string_view` from a function that constructs a local `std::string`, or storing `std::string_view` derived from a `const std::string&` parameter that a caller might satisfy with a temporary, creates immediate dangling references.

```cpp
#include <cstddef>
#include <string>
#include <string_view>

std::string_view trim_prefix(std::string_view sv, std::size_t n) {
    sv.remove_prefix(std::min(n, sv.size()));
    return sv; // Safe only while the caller's backing storage lives
}

void danger_examples() {
    // 1. Dangling: temporary std::string dies at the semicolon
    // std::string_view bad = std::string{"temp"} + "_key";

    // 2. Missing '\0' at sv.data()[sv.size()]
    std::string full = "header:body";
    std::string_view key{full.data(), 6}; // "header" — key.data()[6] is ':', not '\0'
    (void)key;
}
```

> **Standardese.** Prior to C++23, `std::string_view{nullptr}` selected the `const CharT*` constructor and caused immediate undefined behavior when computing string length. C++23 `[string.view.cons]` explicitly defines `basic_string_view(std::nullptr_t) = delete;`, turning null-pointer construction into a compile-time error.

## std::span: mutable or read-only views of contiguous memory

Where `std::string_view` models read-only character sequences, C++20 `std::span<T, Extent>` models a non-owning view over any contiguous sequence of `T` objects: `std::vector<T>`, `std::array<T, N>`, built-in arrays `T[N]`, or `(ptr, count)` pairs.

`std::span` provides two structural modes controlled by its second template parameter:

- **Dynamic extent (`std::span<T>`, default `Extent = std::dynamic_extent`):** stores `{T* ptr, std::size_t count}` (`sizeof == 2 * sizeof(void*)`) and accepts any contiguous range at runtime.
- **Static extent (`std::span<T, N>`):** encodes `N` in the type system, stores only `{T* ptr}` (`sizeof == sizeof(void*)`), and turns mismatched array sizes into compile-time diagnostics.

Crucially, **`const` on `std::span` is shallow**, matching pointer semantics rather than container semantics. `const std::span<int>` prevents rebinding the `span` to another array (`int* const`), while still allowing `s[0] = 42`. To promise read-only elements, put `const` inside the template argument: `std::span<const int>`.

```cpp
#include <array>
#include <cstddef>
#include <span>
#include <vector>

static_assert(sizeof(std::span<int, 4>) == sizeof(int*));
static_assert(sizeof(std::span<int>) == 2 * sizeof(int*));

int sum_first_two(std::span<const int> values) {
    auto head = values.first(values.size() >= 2 ? 2 : values.size());
    int total = 0;
    for (int v : head) {
        total += v;
    }
    return total;
}

void span_constness_and_bytes() {
    std::array<int, 4> arr{1, 2, 3, 4};
    const std::span<int> shallow_const{arr};
    shallow_const[0] = 99; // Well-formed: elements remain mutable

    std::span<const std::byte> raw = std::as_bytes(shallow_const);
    std::span<std::byte> rw_raw = std::as_writable_bytes(shallow_const);
    (void)raw;
    (void)rw_raw;
}
```

`std::as_bytes` and `std::as_writable_bytes` re-slice any span into `std::span<const std::byte>` or `std::span<std::byte>` (when `T` is non-`const`) with `size_bytes()` elements, giving serialization and hashing code a type-safe alternative to C-style `reinterpret_cast<const char*>` pointer arithmetic.

> **Pitfall.** Writing `const std::span<T>` as a function parameter does not prevent the function from mutating the caller's elements. Always write `std::span<const T>` for read-only array parameters.

## Choosing between string, string_view, and span

Selecting the right vocabulary type hinges on ownership, mutability, null-termination requirements, and lifetime scope.

| Type | Ownership | Null-terminated | Element mutability | Typical size | Primary role |
| --- | --- | --- | --- | --- | --- |
| `std::string` | Owning (unique or SSO) | Always (`s[size()] == '\0'`) | Mutable (unless `const`) | 24–32 bytes | Return values, stored fields, mutation |
| `std::string_view` | Non-owning | Not guaranteed | Read-only (`const char`) | 2 words | Read-only string parsing parameters |
| `std::span<const T>` | Non-owning | N/A | Read-only elements | 1 or 2 words | Read-only contiguous slice parameters |
| `std::span<T>` | Non-owning | N/A | Mutable elements | 1 or 2 words | In-place mutation of caller buffers |

Watch the **C++20 Ranges boundary** as well: both `std::string_view` and `std::span` satisfy `std::ranges::borrowed_range` and `std::ranges::view`, meaning algorithms can safely return iterators into rvalue `string_view` or `span` objects because their iterators point to underlying storage independent of the lightweight wrapper. `std::string` is neither a `view` nor a `borrowed_range`, so calling `std::ranges::find` on a temporary `std::string` yields `std::ranges::dangling` instead of a dangling iterator.

## What to remember

- `std::string` owns its bytes, guarantees `s[s.size()] == '\0'`, and invalidates views on reallocation or SSO moves.
- Pass `std::string_view` and `std::span` **by value**; never assume `sv.data()` is null-terminated after slicing or substring construction.
- `const std::span<T>` freezes the span descriptor, not the elements; use `std::span<const T>` for read-only element access.
- `std::span<T, N>` stores a single pointer when the extent `N` is static, while `std::span<T>` stores `{ptr, count}`.
- Both `std::string_view` and `std::span` model `std::ranges::borrowed_range`, shifting lifetime responsibility onto the caller whose storage they borrow.

## Concept Map Outline

- **Strings, views, and spans:** Contiguous sequence types divide into heap-capable owners and lightweight non-owning pointer-length descriptors.
  - **std::string ownership:** Owns a contiguous character buffer uniquely with a guaranteed trailing null terminator.
    - **Small String Optimization:** Short strings live inline inside the object footprint, avoiding heap allocation.
      - **Move invalidation:** Moving an SSO string copies inline bytes, leaving prior views pointing at the source.
    - **Trailing null invariant:** Since C++11, data()[size()] is always CharT(), shared identically by c_str().
    - **resize_and_overwrite:** C++23 method lets C APIs populate capacity directly without pre-zeroing memory.
  - **std::string_view borrowing:** Trivially copyable two-word descriptor of read-only characters passed by value.
    - **Missing null terminator:** Slicing via remove_suffix does not write a null byte, making printf with data() unsafe.
    - **Dangling lifetimes:** Binding a view to a temporary std::string dangles immediately at the semicolon.
    - **Deleted nullptr ctor:** C++23 deletes basic_string_view(nullptr_t) to turn null construction into a compile error.
  - **std::span descriptors:** Non-owning contiguous range wrapper generalizing pointer and count for any element type.
    - **Static vs dynamic extent:** Static extent span<T, N> stores one pointer, whereas dynamic span<T> stores pointer and count.
    - **Shallow constness:** const span<T> allows mutating elements; write span<const T> for read-only views.
    - **Raw byte views:** as_bytes and as_writable_bytes reinterpret contiguous elements as std::byte spans.
  - **API boundary rules:** Return owning containers upward and pass non-owning views downward by value.
    - **borrowed_range opt-in:** Both string_view and span satisfy borrowed_range so range algorithms return real iterators.
    - **Slicing subranges:** Use first, last, and subspan to narrow views in constant time without allocations.

## Standardese Quiz Questions

### Question 1 [warmup] ([span.overview], [views.span])

Assuming 64-bit pointers (8 bytes) and std::size_t (8 bytes), what are the sizes of std::span<int, 4> and std::span<int>?

```cpp
#include <span>
constexpr auto a = sizeof(std::span<int, 4>);
constexpr auto b = sizeof(std::span<int>);
```

A. a is 8 bytes (pointer only) and b is 16 bytes (pointer plus count)
B. Both are 16 bytes because every span stores pointer and count
C. a is 16 bytes (4 ints inline) and b is 16 bytes
D. a is 24 bytes and b is 16 bytes

**Answer (A):** a is 8 bytes (pointer only) and b is 16 bytes (pointer plus count)

**Why:** A static-extent std::span<int, 4> encodes the length 4 in its type, so it only needs to store a single T* pointer (8 bytes). A dynamic-extent std::span<int> stores both T* and std::size_t (16 bytes).

### Question 2 [warmup] ([span.elem], [views.span])

What does the const qualifier on the parameter const std::span<int> s enforce inside the function?

```cpp
void tweak(const std::span<int> s) {
    if (!s.empty()) s[0] = 42;
}
```

A. Compilation fails because s is const and cannot modify elements
B. s[0] returns a prvalue copy, leaving the caller's array unchanged
C. Compilation succeeds and s[0] modifies the caller's underlying int
D. Compilation fails unless s is passed by const reference

**Answer (C):** Compilation succeeds and s[0] modifies the caller's underlying int

**Why:** const on std::span<int> is shallow: it prevents reassigning s itself inside tweak, not mutating the elements it refers to. Read-only element access requires std::span<const int>.

### Question 3 [tricky] ([string.view.modifiers], [string.view.access])

What is the bug in passing sv.data() to std::puts after slicing with remove_suffix?

```cpp
#include <cstdio>
#include <string_view>

void print_prefix(std::string_view sv) {
    sv.remove_suffix(4);
    std::puts(sv.data());
}
```

A. remove_suffix reallocates the underlying buffer without a terminator
B. sv.data() still points to the full original string (or non-null-terminated memory), so std::puts prints past the slice or causes undefined behavior
C. std::string_view::data() returns nullptr after remove_suffix
D. remove_suffix throws std::out_of_range if sv is shorter than 4 characters

**Answer (B):** sv.data() still points to the full original string (or non-null-terminated memory), so std::puts prints past the slice or causes undefined behavior

**Why:** std::string_view::remove_suffix only decrements the internal length without copying bytes or writing a null terminator. Thus sv.data() still points at the unsliced buffer—printing the removed suffix too or overrunning if the original view was not null-terminated.

### Question 4 [tricky] ([string.require], [string.cons])

What happens to sv after moving a short SSO std::string a into b?

```cpp
std::string a = "hello";
std::string_view sv = a;
std::string b = std::move(a);
```

A. sv automatically updates to point into b's buffer
B. sv still safely observes "hello" inside b because move steals a heap pointer
C. std::string_view pins a's buffer via reference counting until sv is destroyed
D. Moving a short SSO string copies inline bytes and invalidates pointers, references, and views into a; sv no longer refers to b and reading it is invalid

**Answer (D):** Moving a short SSO string copies inline bytes and invalidates pointers, references, and views into a; sv no longer refers to b and reading it is invalid

**Why:** Under [string.require], moving a std::basic_string invalidates pointers, references, and iterators into the moved-from string. For short strings using SSO, the characters reside directly inside a's footprint and must be copied into b, leaving sv disconnected from b.

### Question 5 [standardese] ([string.view.cons])

How does C++23 treat the initialization std::string_view sv = nullptr; compared to C++17?

```cpp
#include <string_view>
std::string_view sv = nullptr;
```

A. In C++17 it selects the const char* constructor causing UB; in C++23 it is ill-formed because the nullptr_t constructor is explicitly deleted
B. In both C++17 and C++23 it constructs an empty string_view with size 0
C. In C++23 it throws std::logic_error at runtime
D. In both C++17 and C++23 it is ill-formed due to ambiguous overloads

**Answer (A):** In C++17 it selects the const char* constructor causing UB; in C++23 it is ill-formed because the nullptr_t constructor is explicitly deleted

**Why:** In C++17/C++20, nullptr converted to const char* and invoked basic_string_view(const CharT*), calling Traits::length(nullptr) (UB). C++23 (P2166R1) added basic_string_view(std::nullptr_t) = delete; in [string.view.cons] so it is diagnosed at compile time.

### Question 6 [standardese] ([string.access], [string.require])

Given std::string s = "abc";, what does evaluating s[s.size()] produce in C++11 and later?

```cpp
std::string s = "abc";
char c = s[s.size()];
```

A. Undefined behavior because s.size() is out of bounds
B. Throws std::out_of_range
C. Well-defined: returns a reference to CharT() ('\0'), though writing a non-null value through a non-const reference to s[s.size()] is undefined behavior
D. Well-defined only when s is const-qualified

**Answer (C):** Well-defined: returns a reference to CharT() ('\0'), though writing a non-null value through a non-const reference to s[s.size()] is undefined behavior

**Why:** Since C++11, [string.access] guarantees that both const and non-const operator[](size()) return a reference to CharT() (the null terminator). Reading s[s.size()] is well-defined, while modifying it to any value other than CharT() is undefined behavior.
