TRT
Sign in

Core · Module 7 · Lesson 2 of 11

string: Characters, Storage and Small-String Optimization

Store text without manual new and delete, compare size, capacity and null terminators, and predict when small-string optimization avoids the heap.

Pro — members onlyAbout 10 minutes

By the end you can

  • Explain size, capacity and null terminator guarantees for std::string
  • Predict when short strings avoid heap allocation through SSO
  • Use reserve, c_str, at and operator+= without invalidating views

Builds on vector: The Default Container and How It Grows.

01 · A question first

Your catalogue titles average nine characters, yet every title triggers a heap allocation and your profiler glows. Why does one string allocate while another never touches the heap, where does the terminating null live, and when does a pointer into a string expire?

Make a guess before you read on.

02 · The idea

Members only

Unlock the full lesson

This lesson is part of the membership. Join to read it in full, along with every other premium deep dive.

What's inside

  • Start with the problem
  • How the idea works
  • Why it is correct
  • What it costs
  • Pitfalls and variations
  • How to keep views alive across growth
  • Where it is used

03 · Practice

Something wrong with this lesson? Report a problem