docs(10_Wiki): Dev 폴더 누락분 반영 — Topic_Programming으로 통합
이전 재구성 작업에서 Dev/ 폴더가 누락되었던 것을 반영. - Dev/Topic_Programming(중첩 폴더, 78개)은 Dev 자체 최상위 폴더들 (Architecture/Conventions/Engineering_Intelligence 등)과 완전 중복이라 제거. - Dev 최상위 엔지니어링 지식 폴더(Architecture/Conventions/Engineering_Intelligence/ Failure_Library/Generalized_Principles/Language/Pattern_Catalog/Platform_Guides/ Subsystems, 77개)는 이미 Topic_Programming/Topic_Programming에 더 최신 버전이 존재해 중복 제거(고유 콘텐츠 1개는 예외 처리하여 이동 보존). - Dev의 W3Schools 언어 튜토리얼 폴더(Topic_C/CPP/CSS/CSharp/HOWTO/HTML/Java/ JavaScript/PHP/Python/SQL/W3CSS, 1201개)는 전부 Topic_Programming 하위로 이동. - 에이전트 운영 상태(.astra/docs)는 그대로 유지, 콘텐츠 폴더만 정리. - Topic_Programming 최종 문서 수: 2784 → 3985.
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
---
|
||||
id: c-structs-padding
|
||||
title: "C Struct Alignment and Padding"
|
||||
category: "Programming_Language"
|
||||
status: "draft"
|
||||
verification_status: "conceptual"
|
||||
canonical_id: ""
|
||||
aliases: ["struct padding", "member reordering", "struct vs union size", "C 구조체 패딩"]
|
||||
duplicate_of: ""
|
||||
source_trust_level: "B"
|
||||
confidence_score: 0.87
|
||||
created_at: 2026-07-04
|
||||
updated_at: 2026-07-04
|
||||
review_reason: ""
|
||||
merge_history: []
|
||||
tags: ["c", "programming-language", "w3schools", "structs", "memory-alignment"]
|
||||
raw_sources: ["https://www.w3schools.com/c/c_structs_padding.php"]
|
||||
applied_in: []
|
||||
github_commit: ""
|
||||
---
|
||||
|
||||
# [[C Struct Alignment and Padding]]
|
||||
|
||||
## 🎯 한 줄 통찰 (One-line insight)
|
||||
Simply REORDERING a struct's members — with no change to what data it holds — can shrink its total size by a third: the exact same three members (`char`, `int`, `char`) occupy 12 bytes when declared `char, int, char` but only 8 bytes when reordered to `int, char, char`, because the compiler only needs to insert padding to align the LARGEST member (int) once, at the front, instead of bracketing it awkwardly between two chars. [S1]
|
||||
|
||||
## 🧠 핵심 개념 (Core concepts)
|
||||
- **Padding** — extra, unused bytes the compiler inserts between struct members so each member starts at a memory address aligned to its own size (e.g. an `int` starting at a multiple of 4), because aligned reads are faster for the CPU. [S1]
|
||||
- **Size ≠ sum of members** — a struct with 1+4+1=6 bytes of actual data can occupy 12 bytes total once padding is added. [S1]
|
||||
- **Reordering reduces padding** — placing larger members FIRST (before smaller ones) minimizes the total padding needed, shrinking overall struct size. [S1]
|
||||
- **Structs vs unions: no padding in unions** — a union's members all share ONE memory location (sized to the largest member), so there's no padding BETWEEN members, though alignment rules for the shared location itself still apply. [S1]
|
||||
|
||||
## 📖 세부 내용 (Details)
|
||||
- Unpadded-looking struct that's actually 12 bytes: `struct Example { char a; int b; char c; };` — expected 6 bytes, actual 12 (1 + 3 padding + 4 + 1 + 3 padding, so `b` starts at a 4-byte-aligned offset and the total is also a multiple of 4). [S1]
|
||||
- Reordered struct shrinking to 8 bytes: `struct Example { int b; char a; char c; };` — same three members, less padding needed since the int comes first. [S1]
|
||||
- Struct vs union size contrast with identical members: `struct S { char a; int b; char c; };` (12 bytes) versus `union U { char a; int b; char c; };` (4 bytes, sized to the largest member `int`). [S1]
|
||||
|
||||
## ⚖️ 모순 및 업데이트 (Contradictions & updates)
|
||||
- **멤버 순서만으로 구조체 크기가 달라짐**: 동일한 세 멤버라도 char-int-char 순서면 12바이트, int-char-char 순서면 8바이트가 된다는 점이 직접 비교로 증명되며, 큰 타입을 앞에 배치하면 패딩을 줄일 수 있다는 실용적 팁으로 이어짐. [S1]
|
||||
|
||||
## 🛠️ 적용 사례 (Applied in summary)
|
||||
저수준 메모리나 파일 포맷을 다루지 않는 한 이 문제를 신경 쓸 필요는 거의 없다고 원문에 명시되지만, 메모리가 중요한 대규모 프로그램에서는 멤버 순서 재배치로 구조체 크기를 줄이는 것이 실전 최적화 기법으로 제시됨. [S1]
|
||||
|
||||
## 💻 코드 패턴 (Code patterns)
|
||||
Reordering struct members (larger types first) to reduce padding and shrink total size (C):
|
||||
```c
|
||||
// 12 bytes (char, int, char — more padding needed)
|
||||
struct Example { char a; int b; char c; };
|
||||
|
||||
// 8 bytes (int, char, char — less padding needed)
|
||||
struct Example { int b; char a; char c; };
|
||||
```
|
||||
|
||||
## ✅ 검증 상태 및 신뢰도
|
||||
- **상태:** draft
|
||||
- **검증 단계:** conceptual
|
||||
- **출처 신뢰도:** B (W3Schools — widely used educational reference, not a primary standards body)
|
||||
- **신뢰 점수:** 0.87
|
||||
- **중복 검사 결과:** 신규 생성 (New discovery)
|
||||
|
||||
## 🔗 지식 그래프 (Knowledge Graph)
|
||||
- **상위/루트:** [[C Tutorial]]
|
||||
- **관련 개념:** [[C Structs Nested]], [[C Unions]], [[C Structs Pointers]]
|
||||
- **참조 맥락:** 구조체 정렬/패딩 — 구조체와 포인터(Structs Pointers) 챕터로 이어짐, 유니온(Unions) 챕터와 직접 대비됨.
|
||||
|
||||
## 📚 출처 (Sources)
|
||||
- [S1] W3Schools — C Struct Alignment and Padding — https://www.w3schools.com/c/c_structs_padding.php
|
||||
|
||||
## 📝 변경 이력 (Change history)
|
||||
- 2026-07-04: Initial draft synthesized from the W3Schools "C Struct Alignment and Padding" page (Astra wiki-curation, P-Reinforce v3.1 format).
|
||||
Reference in New Issue
Block a user