---
id: cpp-arrays-size
title: "C++ Array Size"
category: "Programming_Language"
status: "draft"
verification_status: "conceptual"
canonical_id: ""
aliases: ["sizeof array", "array length formula", "foreach vs sizeof loop", "C++ 배열 크기"]
duplicate_of: ""
source_trust_level: "B"
confidence_score: 0.86
created_at: 2026-07-04
updated_at: 2026-07-04
review_reason: ""
merge_history: []
tags: ["cpp", "programming-language", "w3schools", "arrays", "sizeof"]
raw_sources: ["https://www.w3schools.com/cpp/cpp_arrays_size.asp"]
applied_in: []
github_commit: ""
---

# [[CPP Array Size]]

## 🎯 한 줄 통찰 (One-line insight)
This chapter explicitly presents THREE competing ways to loop through an array — hardcoded bound (fragile), `sizeof(arr)/sizeof(arr[0])` (robust but verbose), and the C++11 for-each loop (cleanest) — and closes by saying it's "good to know" all three because "you may encounter them all in different programs," meaning C++ codebases in the wild mix loop styles from different eras, unlike C where the sizeof-formula approach was presented as the single best practice. [S1]

## 🧠 핵심 개념 (Core concepts)
- **`sizeof(array)`** — returns the array's TOTAL memory footprint in bytes, not element count (same C behavior). [S1]
- **Element-count formula** — `sizeof(arr) / sizeof(arr[0])` gives the actual element count. [S1]
- **Three loop styles for arrays** — hardcoded bound (fragile), sizeof-formula bound (robust), for-each (C++11, cleanest). [S1]
- **All three coexist in real code** — the tutorial explicitly recommends knowing all of them since different programs use different styles. [S1]

## 📖 세부 내용 (Details)
- Raw byte-size result: `int myNumbers[5] = {10, 20, 30, 40, 50}; cout << sizeof(myNumbers); // 20 (not 5)`. [S1]
- Correct element-count formula: `int getArrayLength = sizeof(myNumbers) / sizeof(myNumbers[0]); cout << getArrayLength; // 5`. [S1]
- Sizeof-driven loop (robust but verbose): `for (int i = 0; i < sizeof(myNumbers) / sizeof(myNumbers[0]); i++) { cout << myNumbers[i] << "\n"; }`. [S1]
- The cleanest alternative (C++11 for-each): `for (int num : myNumbers) { cout << num << "\n"; }`. [S1]

## ⚖️ 모순 및 업데이트 (Contradictions & updates)
- **세 가지 반복 스타일이 실전에서 혼재함**: 하드코딩된 경계, sizeof 공식 기반, for-each 세 방식 모두 실제 프로그램에서 마주칠 수 있어 전부 알아두는 것이 좋다고 명시적으로 권고됨 — C 챕터가 sizeof 공식을 유일한 권장 방식으로 제시한 것과 대비됨. [S1]

## 🛠️ 적용 사례 (Applied in summary)
현재 발견된 실제 적용 사례가 없습니다 — 세 가지 반복 스타일을 모두 이해해두는 것이 다양한 C++ 코드베이스를 읽는 실전 능력으로 이어진다. [S1]

## 💻 코드 패턴 (Code patterns)
The cleanest way to loop through an array, introduced in C++11 (C++):
```cpp
int myNumbers[5] = {10, 20, 30, 40, 50};
for (int num : myNumbers) {
  cout << num << "\n";
}
```

## ✅ 검증 상태 및 신뢰도
- **상태:** draft
- **검증 단계:** conceptual
- **출처 신뢰도:** B (W3Schools — widely used educational reference, not a primary standards body)
- **신뢰 점수:** 0.86
- **중복 검사 결과:** 신규 생성 (New discovery)

## 🔗 지식 그래프 (Knowledge Graph)
- **상위/루트:** [[C++ Tutorial]]
- **관련 개념:** [[CPP Arrays]], [[CPP Arrays Loop]], [[CPP For Loop Foreach]], [[C Arrays Size]]
- **참조 맥락:** 배열 크기 계산 공식 — 배열과 반복문(Arrays Loop) 챕터로 이어짐.

## 📚 출처 (Sources)
- [S1] W3Schools — C++ Array Size — https://www.w3schools.com/cpp/cpp_arrays_size.asp

## 📝 변경 이력 (Change history)
- 2026-07-04: Initial draft synthesized from the W3Schools "C++ Array Size" page (Astra wiki-curation, P-Reinforce v3.1 format).
