A backing array is the internal array that stores a resizable vector’s elements. When it fills, the vector allocates a larger array, copies the existing elements, and continues adding without user-written memory management. Repeated growth can be expensive, so reserve an estimated capacity when the required size is reasonably known. section 5.10
Mistake: choosing a fixed array for an unknown count
A fixed Person array requires a predetermined size and default-initialized slots, so unused positions can contain meaningless data. Replacing it with std::vector<Person> lets the program push actual people as needed; calling reserve(count) reduces avoidable backing-array reallocations while preserving correct behavior if the estimate is wrong. section 5.10
Listing 5.22 Static arrays force using a default constructor
struct Person {
std::string name;
int age;
Person(const std::string& n, int a) : name(n), age(a) {}
Person() : name("", 0) {}
};
int main() {
Person people[3]; #1
Person suzy("Susan", 25);
people[0] = suzy;
Person anna("Annette", 32);
people[2] = anna;
std::cout << people[1].name << '\n';
int count = 5; // assume this is computed
Person* others = new Person[count]; #2
return 0;
}
Compare the fixed Person array, its forced default constructor, and the manually allocated array that motivates a safer vector-based design. section 5.10


