C++ Array vs vector: Performance, Safety, and When to Use
이 글의 핵심
C-style arrays, std::array, and std::vector compared: stack vs heap, fixed vs dynamic size, bounds checking, benchmarks, and practical choice guide.
For arrays vs linked lists in algorithm interviews (cache locality, insertion cost), see [arrays and lists](/en/blog/algorithm-series-01-array-list/—it complements choosing among C++ array flavors here.
Introduction: “Array or vector?"
"I heard arrays are faster, but everyone says use vector”
C++ offers C-style arrays, std::array, and std::vector. They differ in storage, resize capability, and safety.
int arr1[5] = {1, 2, 3, 4, 5};
std::array<int, 5> arr2 = {1, 2, 3, 4, 5};
std::vector<int> vec = {1, 2, 3, 4, 5};
This article covers:
- Differences among the three
- Performance benchmarks
- Memory safety
- Selection guide
Table of contents
1. Three array kinds
Comparison table
| Aspect | C array | std::array | std::vector |
|---|---|---|---|
| Storage | Usually stack | Usually stack | Heap |
| Size | Fixed (compile-time) | Fixed (compile-time) | Dynamic |
| Bounds check | No | at() | at() |
| Size query | sizeof hacks | size() | size() |
| STL algorithms | Partial | Full | Full |
| Function args | Decays to pointer | By value/ref | Typically const ref |
| Safety | Low | High | High |
C-style array pitfalls
int arr[5] = {1, 2, 3, 4, 5};
// arr[10] = 99; // undefined behavior
void foo(int arr[]) { /* sizeof(arr) is pointer size */ }
std::array (C++11)
#include <array>
std::array<int, 5> arr = {1, 2, 3, 4, 5};
arr.at(10); // throws std::out_of_range
std::sort(arr.begin(), arr.end());
std::vector
#include <vector>
std::vector<int> vec = {1, 2, 3, 4, 5};
vec.push_back(6);
vec.resize(10);
vec.at(10); // throws if out of range
2. Benchmarks
Test 1: element access
Result (typical optimized builds): same order of magnitude for indexed access over large iteration counts.
Test 2: create/destroy in a loop
Stack arrays / std::array: cheap construction.
vector: heap allocation/deallocation dominates if you repeatedly create huge vectors in inner loops.
Test 3: iteration
Range-for and indexed loops are typically comparable when optimized.
3. Memory safety
Bounds checking
Use at() when you want exceptions on out-of-range access; operator[] is unchecked (fast, sharp).
Passing arrays to functions
Prefer std::array<T,N> or const std::vector& so length travels with the data.
4. Selection guide
Decision sketch
Fixed size known at compile time?
No -> std::vector
Yes -> Small (< ~100) -> std::array often fine
Large buffer -> vector (avoid huge stack frames)
Recommendations
| Situation | Prefer |
|---|---|
| Default | std::vector |
| Small fixed buffer, hot ctor/dtor | std::array |
| Dynamic size | std::vector |
| Interop with C APIs | raw arrays only at the boundary |
Examples
Fixed small buffer:
std::array<char, 1024> buffer;
readData(buffer.data(), buffer.size());
Dynamic list:
std::vector<int> numbers;
numbers.reserve(100);
for (int i = 0; i < n; ++i) numbers.push_back(i);
3D coordinate:
struct Position {
std::array<float, 3> coords;
};
Summary
Default: std::vector.
Fixed small N: std::array.
Avoid raw C arrays except FFI boundaries.
Next: read the vector deep-dive in the series.
Related reading
Keywords
array vs vector, std::array, stack vs heap, C++ performance
Practical tips
- Use
at()in debug-heavy workflows; keep[]where you have proven invariants. reserve()for known growth patterns.- Watch stack size for large
std::arraylocals.
Related posts
Frequently Asked Questions (FAQ)
Q. When would I use this in practice?
A. C-style arrays, std::array, and std::vector compared: stack vs heap, fixed vs dynamic size, bounds checking.
Q. What should I read before this?
A. Follow the previous article or related articles links at the bottom of each post to learn in sequence. See the C++ series index for the full picture.
Q. Where can I study this more deeply?
A. Check cppreference and the relevant library’s official documentation. The reference links at the end of the article are also worth using.
Related Articles (Internal Links)
Other articles related to this topic.
- Arrays and Lists
- C++ vector 기초 완벽 가이드 | 초기화·연산·용량 관리와 실전 패턴
- C++ 스택 vs 힙 | 재귀에서 프로그램이 죽는 이유와 스택 오버플로우 사례
Keywords Covered in This Article (Related Search Terms)
This article covers C++, array, vector, std::array, performance, memory safety, STL.