[2026] C++ Array vs vector: Performance, Safety, and When to Use Each

[2026] C++ Array vs vector: Performance, Safety, and When to Use Each

이 글의 핵심

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—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
  2. Benchmarks
  3. Memory safety
  4. Selection guide
  5. Summary

1. Three array kinds

Comparison table

AspectC arraystd::arraystd::vector
StorageUsually stackUsually stackHeap
SizeFixed (compile-time)Fixed (compile-time)Dynamic
Bounds checkNoat()at()
Size querysizeof hackssize()size()
STL algorithmsPartialFullFull
Function argsDecays to pointerBy value/refTypically const ref
SafetyLowHighHigh

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)

아래 코드는 cpp를 사용한 구현 예제입니다. 필요한 모듈을 import하고, 에러 처리를 통해 안정성을 확보합니다. 코드를 직접 실행해보면서 동작을 확인해보세요.

#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

아래 코드는 cpp를 사용한 구현 예제입니다. 필요한 모듈을 import하고, 에러 처리를 통해 안정성을 확보합니다. 코드를 직접 실행해보면서 동작을 확인해보세요.

#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

다음은 간단한 text 코드 예제입니다. 코드를 직접 실행해보면서 동작을 확인해보세요.

Fixed size known at compile time?
  No  -> std::vector
  Yes -> Small (< ~100) -> std::array often fine
         Large buffer    -> vector (avoid huge stack frames)

Recommendations

SituationPrefer
Defaultstd::vector
Small fixed buffer, hot ctor/dtorstd::array
Dynamic sizestd::vector
Interop with C APIsraw 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.

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::array locals.

... 996 lines not shown ... Token usage: 63706/1000000; 936294 remaining Start-Sleep -Seconds 3