Introduction
Using std::vector is easy; writing one teaches you how C++ really manages objects. Memory allocation and object lifetime are separate steps, moving is not the same as copying, and a careless line can leak objects or destroy them twice when an exception is thrown. The harness counts every construction, copy, move and destruction to prove the container is correct.
Problem statement
push_back(a[0]) must be safe during reallocation, and a throwing copy must leave everything unchanged and leak nothing. Write a scenario-based test harness that verifies all of this.Objectives
- Separate allocation (operator new) from construction (placement new)
- Implement the Rule of Five and copy-and-swap
- Provide the strong exception guarantee
- Understand std::move_if_noexcept and why vector uses it
Features
- Geometric (doubling) growth
- Iterators that work with std::sort, std::find and std::accumulate
- Exception-safe copy and reallocation
- Self-referencing push_back is safe
- 8 test scenarios with pass/fail checks and object counters
Concepts demonstrated
- Rule of Five
- Placement new
- Exception safety guarantees
- Move semantics
- Iterators
- Instrumented testing
System architecture
DynArray<T> holds T* data_, size_ and capacity_. Memory comes from ::operator new (raw bytes) and objects are created with placement new and destroyed with explicit destructor calls, so capacity can exceed size. transfer_to builds elements in a new buffer with std::move_if_noexcept; if any construction throws it destroys what it built and frees the buffer, leaving the original untouched. emplace_back constructs the new element in the new buffer *before* moving the old ones, which makes push_back(a[0]) safe. Assignment takes its argument by value and swaps (copy-and-swap). The harness's Tracked type counts lifecycle events; CopyOnly has a throwing move.
Folder structure
Complete source code
Makefile
What each file does
- src/dyn_array.h
- The complete
DynArray<T>template: storage management, Rule of Five, growth, modifiers and iterators. - src/main.cpp
- Instrumented test types and the eight named scenarios with their checks.
- Makefile
- Build automation:
makebuilds./app,make testruns the sample session and compares it with the expected output,make cleanremoves build files.
How to run it
Build
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
# run one scenario by name, or "all" growth not-a-scenario all
Testing
- Compare
./app < tests/sample-input.txtwithtests/expected-output.txt; the run ends with '0 check(s) failed'. - growth: capacities go 1 2 4 4 8 8 8 8 16.
- realloc-moves: five emplace_backs cause exactly 7 moves and 0 copies; a type with a throwing move is copied 7 times instead.
- exception-safety: when the second copy throws, the source is unchanged and every object is destroyed.
Common errors
Objects destroyed twice / heap corruption
After moving elements to a new buffer you must still destroy the moved-from originals exactly once, then free the old raw memory with operator delete (not delete[]).
Memory leak when a copy constructor throws
A constructor that throws never runs its own destructor; catch, clean up what was built, and rethrow (see copy_from).
Possible improvements
- Add insert(pos, value) and resize(n)
- Support custom allocators
- Add a small-buffer optimisation for short arrays
Additional challenges
- 1Make the iterators a class with debug checks for out-of-range access.
- 2Add emplace(pos, args...) with the strong guarantee.
- 3Benchmark against std::vector with a million push_backs.
Built and tested it yourself? Mark it to update your dashboard.