C++ CPP study materials
C++ CPP study resources

Reserve does not create ten elements

C++ CPP · Put your knowledge to work in context.

All case studies

Reserve does not create ten elements

A fictional C++ exercise starts with an empty vector and calls reserve with an argument of ten. A learner claims the vector now contains ten initialized elements and that its reported capacity must equal exactly ten on every conforming implementation. Another reads the capacity and size contracts separately and asks how a later indexed write would be justified. The exercise supplies no element insertion or resizing operation. This written case allocates no production resource and changes no program. It asks which guarantees actually follow, why the absence of an allocation failure does not create valid element positions and how a test should distinguish minimum capacity from an exact implementation-selected allocation.

1. What does reserve establish if it succeeds?

Read the worked answer

The size remains zero and the relevant capacity is at least the requested amount under the operation’s rules. The exact capacity need not equal ten.

2. Why is direct element access not justified?

Read the worked answer

No elements have been created by reserve. A valid insertion or resize and correct bounds are required before accessing an element position; allocated capacity is not the same as size.