docs(10_Wiki): 위키 구조 정리 — 언어 튜토리얼 카테고리 폴더 제거 + 신규 자산 동기화
Topic_CSS/Topic_HTML/Topic_JavaScript/Topic_Prompt/Topic_Comfyui 등 기존 카테고리 폴더를 정리하고, Topic_Graphic/Dev 등 신규 산출물과 Topics 내부 세션/메모리 기록을 동기화.
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
---
|
||||
id: c-memory-struct
|
||||
title: "C Structures and Dynamic Memory"
|
||||
category: "Programming_Language"
|
||||
status: "draft"
|
||||
verification_status: "conceptual"
|
||||
canonical_id: ""
|
||||
aliases: ["malloc struct", "array of structs", "realloc struct array", "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: ["c", "programming-language", "w3schools", "memory-management", "structs"]
|
||||
raw_sources: ["https://www.w3schools.com/c/c_memory_struct.php"]
|
||||
applied_in: []
|
||||
github_commit: ""
|
||||
---
|
||||
|
||||
# [[C Structures and Dynamic Memory]]
|
||||
|
||||
## 🎯 한 줄 통찰 (One-line insight)
|
||||
Growing a dynamically-allocated array of structs with `realloc()` only extends the ALLOCATION — the newly added slots are explicitly UNINITIALIZED, meaning code that grows a 2-car array to 3 cars must manually `strcpy()`/assign values into the new index (`cars[2]`) before using it, or risk reading garbage; `realloc()` never zero-fills or default-constructs the extra space it creates. [S1]
|
||||
|
||||
## 🧠 핵심 개념 (Core concepts)
|
||||
- **`malloc(sizeof(struct Type))`** — allocates memory for exactly ONE struct instance, accessed via a pointer and the `->` arrow operator. [S1]
|
||||
- **`malloc(N * sizeof(struct Type))`** — allocates memory for an ARRAY of N structs at once, accessible via normal bracket indexing (`cars[0].brand`). [S1]
|
||||
- **`strcpy()` still required for string members** — even in dynamically allocated structs, `char[]` fields can't be assigned via `=` and still need `strcpy()`. [S1]
|
||||
- **Growing with `realloc()` + temporary pointer** — resizing a struct array follows the same safe pattern as the previous chapter (store the result in a temp pointer, check for NULL, only commit on success). [S1]
|
||||
- **New space from `realloc()` is uninitialized** — expanding from 2 to 3 struct elements leaves the new element's fields with undefined content until explicitly assigned. [S1]
|
||||
|
||||
## 📖 세부 내용 (Details)
|
||||
- Allocating memory for one struct: `struct Car *ptr = (struct Car*) malloc(sizeof(struct Car)); if (ptr == NULL) { ... } strcpy(ptr->brand, "Honda"); ptr->year = 2022; free(ptr);`. [S1]
|
||||
- Allocating an array of 3 structs at once: `struct Car *cars = (struct Car*) malloc(3 * sizeof(struct Car)); strcpy(cars[0].brand, "Ford"); cars[0].year = 2015;`. [S1]
|
||||
- Safely growing the array from 2 to 3 elements, then initializing the new slot: `struct Car *tmp = (struct Car*) realloc(cars, newCount * sizeof(struct Car)); if (tmp == NULL) { free(cars); ... } cars = tmp; strcpy(cars[2].brand, "Kia"); cars[2].year = 2022;` — the new element MUST be manually initialized since realloc leaves it undefined. [S1]
|
||||
|
||||
## ⚖️ 모순 및 업데이트 (Contradictions & updates)
|
||||
- **realloc으로 늘어난 공간은 초기화되지 않음**: 배열을 2개에서 3개로 늘려도 새로 생긴 3번째 요소의 필드는 값이 정의되지 않은 상태이므로, 사용 전에 반드시 직접 초기화(strcpy/대입)해야 한다는 점이 명시적으로 경고됨. [S1]
|
||||
|
||||
## 🛠️ 적용 사례 (Applied in summary)
|
||||
자동차 대수가 고정되지 않은 중고차 딜러십 프로그램에서 필요한 만큼만 구조체 메모리를 동적으로 할당하는 것이 원문에서 직접 실전 활용 사례로 제시됨. [S1]
|
||||
|
||||
## 💻 코드 패턴 (Code patterns)
|
||||
Growing a struct array and initializing the newly added, uninitialized slot (C):
|
||||
```c
|
||||
struct Car *tmp = (struct Car*) realloc(cars, newCount * sizeof(struct Car));
|
||||
if (tmp == NULL) {
|
||||
free(cars);
|
||||
printf("Reallocation failed.\n");
|
||||
return 1;
|
||||
}
|
||||
cars = tmp;
|
||||
// Must manually initialize the new element — realloc leaves it undefined
|
||||
strcpy(cars[2].brand, "Kia");
|
||||
cars[2].year = 2022;
|
||||
```
|
||||
|
||||
## ✅ 검증 상태 및 신뢰도
|
||||
- **상태:** draft
|
||||
- **검증 단계:** conceptual
|
||||
- **출처 신뢰도:** B (W3Schools — widely used educational reference, not a primary standards body)
|
||||
- **신뢰 점수:** 0.86
|
||||
- **중복 검사 결과:** 신규 생성 (New discovery)
|
||||
|
||||
## 🔗 지식 그래프 (Knowledge Graph)
|
||||
- **상위/루트:** [[C Tutorial]]
|
||||
- **관련 개념:** [[C Memory Reallocate]], [[C Structs Pointers]], [[C Memory RealLife]]
|
||||
- **참조 맥락:** 구조체 동적 메모리 — 메모리 관리 실전 예제(Memory RealLife) 챕터로 이어짐.
|
||||
|
||||
## 📚 출처 (Sources)
|
||||
- [S1] W3Schools — C Structures and Dynamic Memory — https://www.w3schools.com/c/c_memory_struct.php
|
||||
|
||||
## 📝 변경 이력 (Change history)
|
||||
- 2026-07-04: Initial draft synthesized from the W3Schools "C Structures and Dynamic Memory" page (Astra wiki-curation, P-Reinforce v3.1 format).
|
||||
Reference in New Issue
Block a user