C++ vector reserve vs resize: When to Use Which
이 글의 핵심
C++ vector reserve vs resize: reserve grows capacity only; resize changes length and initializes elements. Reduce reallocations vs pre-fill—performance and when-to-use guide.
Contiguous dynamic arrays (like std::vector) are the usual way to implement the “array” side of [arrays and lists](/en/blog/algorithm-series-01-array-list/—this article focuses on capacity vs length inside that model.
Introduction: “Should I use reserve or resize?"
"Don’t both make the vector bigger?”
In C++, vector::reserve and resize do completely different things. reserve only increases capacity; resize changes size and initializes elements.
std::vector<int> vec;
// reserve: capacity only
vec.reserve(100);
std::cout << vec.size() << '\n'; // 0
std::cout << vec.capacity() << '\n'; // 100
// resize: size + initialization
vec.resize(100);
std::cout << vec.size() << '\n'; // 100
std::cout << vec.capacity() << '\n'; // 100
This article covers:
- reserve vs resize
- Performance comparison
- Usage scenarios
- Practical tips
Table of contents
1. reserve vs resize
reserve: capacity only
std::vector<int> vec;
vec.reserve(5); // capacity at least 5
std::cout << vec.size() << '\n'; // 0 (unchanged)
std::cout << vec.capacity() << '\n'; // 5
// vec[0] = 42; // undefined behavior (size is 0)
vec.push_back(10); // OK
std::cout << vec.size() << '\n'; // 1
resize: size change + initialization
std::vector<int> vec;
vec.resize(5); // size 5, value-initialized to 0
std::cout << vec.size() << '\n'; // 5
std::cout << vec.capacity() << '\n'; // at least 5
vec[0] = 42; // OK
std::cout << vec[0] << '\n'; // 42
std::cout << vec[1] << '\n'; // 0 (initialized)
Comparison table
| Aspect | reserve(n) | resize(n) |
|---|---|---|
| size | Unchanged | becomes n |
| capacity | at least n | at least n |
| Initialization | None | default value |
| Index access | Only within size | Yes |
| push_back | Yes | Yes |
2. Performance
Benchmark: push_back
#include <benchmark/benchmark.h>
// No reserve
static void BM_PushBack_NoReserve(benchmark::State& state) {
for (auto _ : state) {
std::vector<int> vec;
for (int i = 0; i < 1000000; ++i) {
vec.push_back(i);
}
}
}
BENCHMARK(BM_PushBack_NoReserve);
// With reserve
static void BM_PushBack_Reserve(benchmark::State& state) {
for (auto _ : state) {
std::vector<int> vec;
vec.reserve(1000000);
for (int i = 0; i < 1000000; ++i) {
vec.push_back(i);
}
}
}
BENCHMARK(BM_PushBack_Reserve);
// resize
static void BM_Resize(benchmark::State& state) {
for (auto _ : state) {
std::vector<int> vec;
vec.resize(1000000);
for (int i = 0; i < 1000000; ++i) {
vec[i] = i;
}
}
}
BENCHMARK(BM_Resize);
Results (GCC 13, -O3):
BM_PushBack_NoReserve 50 ms (many reallocations)
BM_PushBack_Reserve 10 ms (no reallocations)
BM_Resize 8 ms (fastest)
3. Usage scenarios
reserve: before push_back
// reserve: grow with push_back
std::vector<int> vec;
vec.reserve(1000);
for (int i = 0; i < 1000; ++i) {
vec.push_back(i);
}
resize: index access
// resize: direct indexing
std::vector<int> vec;
vec.resize(1000);
for (int i = 0; i < 1000; ++i) {
vec[i] = i;
}
resize: fill with a value
// resize: initialize to a specific value
std::vector<int> vec;
vec.resize(1000, 42);
std::cout << vec[0] << '\n'; // 42
std::cout << vec[999] << '\n'; // 42
4. Examples
Example 1: reading a file
// reserve: unknown line count
std::vector<std::string> lines;
lines.reserve(1000);
std::ifstream file("data.txt");
std::string line;
while (std::getline(file, line)) {
lines.push_back(line);
}
Example 2: converting from an array
// resize: known size
int arr[100] = {/* ....*/};
std::vector<int> vec;
vec.resize(100);
for (int i = 0; i < 100; ++i) {
vec[i] = arr[i];
}
Example 3: 2D matrix
// resize: 2D vector
std::vector<std::vector<int>> matrix;
matrix.resize(10);
for (auto& row : matrix) {
row.resize(20, 0);
}
matrix[5][10] = 42;
Summary
Choosing reserve vs resize
| Situation | Use |
|---|---|
| Growing with push_back | reserve |
| Direct index access | resize |
| Unknown final size | reserve |
| Known size + initialization | resize |
| Avoid reallocations only | resize |
| Need element initialization | resize |
Rules of thumb
- push_back → reserve
- Index access → resize
- Need initialization → resize
- Reallocations only → reserve
Checklist
- If you use push_back, do you reserve?
- If you index, do you resize first?
- Do you need default initialization?
- Is performance critical?
Related reading (internal links)
- C++ vector basics guide
- C++ vector vs list vs deque
- C++ performance optimization
- C++ STL containers guide
Keywords (SEO)
vector reserve, vector resize, reserve vs resize, vector performance, avoid reallocation
Practical tips
Debugging
- Indexing after reserve only (size still 0) is undefined behavior.
- push_back after resize still grows size.
- capacity rarely shrinks without shrink_to_fit.
Performance
- reserve before push_back loops to avoid reallocations.
- Index-heavy code can be faster with resize + assignment.
- If you do not need initialization, prefer reserve + push_back.
Code review
- Check reserve in hot push_back loops.
- Find index writes without prior resize.
- Replace unnecessary resize with reserve when only capacity matters.
Closing
reserve avoids reallocations; resize changes length and initializes. Takeaways:
- push_back → reserve
- Index access → resize
- Hot paths → reserve If you use push_back, reserve is the usual way to win performance. Next: dig deeper in the C++ STL container guides.
Related posts
자주 묻는 질문 (FAQ)
Q. 이 내용을 실무에서 언제 쓰나요?
A. C++ vector reserve vs resize: reserve grows capacity only; resize changes length and initializes elements. Reduce reallo… 실무에서는 위 본문의 예제와 선택 가이드를 참고해 적용하면 됩니다.
Q. 선행으로 읽으면 좋은 글은?
A. 각 글 하단의 이전 글 또는 관련 글 링크를 따라가면 순서대로 배울 수 있습니다. C++ 시리즈 목차에서 전체 흐름을 확인할 수 있습니다.
Q. 더 깊이 공부하려면?
A. cppreference와 해당 라이브러리 공식 문서를 참고하세요. 글 말미의 참고 자료 링크도 활용하면 좋습니다.
같이 보면 좋은 글 (내부 링크)
이 주제와 연결되는 다른 글입니다.
- [Arrays and Lists](/en/blog/algorithm-series-01-array-list/
- C++ vector vs list vs deque | ‘어떤 컨테이너?’ 성능 비교와 선택 가이드
- C++ 성능 최적화 | ‘10배 빠르게’ 실전 기법
이 글에서 다루는 키워드 (관련 검색어)
C++, vector, reserve, resize, memory, performance, STL 등으로 검색하시면 이 글이 도움이 됩니다.