C++
Object lifetimes, templates, standard library and C++ build errors.
55 troubleshooting guides · Page 1 / 3
Why is vector[i] invalid after reserve without resize?
Reserve changes capacity, not the number of constructed elements. A vector with size zero still has no element at index zero.
Why does a vector element reference break after push_back?
Growth can reallocate vector storage and invalidate its old references, pointers and iterators. A cached address is not stable across arbitrary insertion.
How do I erase vector elements without skipping or invalidating iteration?
Erase shifts later elements and invalidates iterators at or after the erased position. Incrementing that old iterator is not a valid next-step operation.
Why does changing auto item leave the container unchanged?
For ordinary value elements, a by-value range-for variable is a copy. Changing it does not update the stored object.
Why does auto from vector<bool> behave differently from bool?
vector<bool> may return a proxy representing a stored bit rather than a normal bool reference. An auto variable can preserve that connection to the container.
Why does std::remove leave vector size unchanged?
The algorithm compacts retained elements and returns a new logical end. It does not own the container or change its physical size.
Why does checking map[key] insert an unexpected value?
Subscript is an access-or-insert operation. Using it to check a missing key can grow the map and create a default mapped value.
Why does a case-insensitive unordered_map miss equivalent keys?
Equivalent keys must produce equal hashes. Supplying case-insensitive equality with an unchanged case-sensitive hash violates that relationship.
Why does unordered_map rehash invalidate iterators but not element references?
Rehash changes bucket traversal, so iterator validity differs from the lifetime of the actual element object.
Why does <= in a sort comparator produce erratic results?
A sorting comparator must describe a strict ordering. Returning true for equal values breaks that contract.
Why does lower_bound fail after sorting by a different field?
The range must satisfy the search comparison’s partitioning requirement. Sorting by display name does not prepare a range for binary search by numeric identifier.
Why does returning string_view from a temporary string produce garbage?
string_view borrows characters without owning or extending their lifetime. A view into a temporary or local string can outlive its source immediately.
Why can string_view.data() send extra text to a C string API?
A view can end before its backing string’s null terminator. data() communicates an address, not the view’s length.
Why does editing a string invalidate its saved c_str pointer?
The pointer designates storage owned by the string. Mutating operations can invalidate that storage, so the pointer is not a snapshot.
Why can I not copy unique_ptr into another owner?
unique_ptr encodes exclusive ownership. An ordinary copy would create two cleanup responsibilities for the same resource, so that operation is intentionally unavailable.
Why does unique_ptr.release leak my object?
release returns the raw pointer and stops owning it; it does not destroy the resource. Ignoring the result leaves cleanup unassigned.
Why does a second shared_ptr built from get() double-delete?
The raw-pointer constructor creates another control block. Two unrelated ownership groups can then each attempt to destroy the same address.
Why are mutually linked shared_ptr objects never destroyed?
A strong-reference cycle can keep every count above zero after external owners disappear. Reference counting is not a cycle collector.
Why is expired() followed by weak pointer access a race?
Checking expiration and acquiring ownership are separate moments. The last strong owner can disappear between them.
Why does shared_from_this fail in a constructor?
During construction the object may not yet participate in the completed ownership mechanism required by shared_from_this. A raw new expression alone does not establish it.
Why does std::move on a const value still copy?
std::move changes expression category; it neither removes const nor forces a move overload to exist. A normal non-const rvalue-reference move constructor cannot consume a const source.
Can I assume a moved-from std::string is empty?
The general moved-from guarantee for many library types is valid but unspecified state unless a stronger rule is documented. Observing empty in one implementation is not a business postcondition.
Why can return std::move(local) reduce return-value optimization?
Returning a named local directly can permit named return value optimization. Wrapping it with std::move can prevent that particular optimization rather than improve it.
Why does copying a class with a raw owning pointer double-free?
A generated copy operation copies the pointer value, not the allocation it owns. Two objects can then attempt to release one resource.
Ask your own question · Sign in to post questions and answers.