---
id: cpp-memory-management-new
title: "C++ new and delete"
category: "Programming_Language"
status: "draft"
verification_status: "conceptual"
canonical_id: ""
aliases: ["new keyword", "delete keyword", "new[] delete[]", "dynamic arrays", "C++ new와 delete"]
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: ["cpp", "programming-language", "w3schools", "memory-management", "new", "delete"]
raw_sources: ["https://www.w3schools.com/cpp/cpp_memory_management_new.asp"]
applied_in: []
github_commit: ""
---

# [[CPP new and delete]]

## 🎯 한 줄 통찰 (One-line insight)
C++'s `new`/`delete` pair REPLACES C's `malloc()`/`free()` pair entirely, but with a critical syntactic split C never had: single values use plain `new`/`delete`, while ARRAYS require the bracketed `new[]`/`delete[]` forms specifically — mismatching them (using plain `delete` on an array allocated with `new[]`) is exactly the kind of subtle bug this language-level distinction exists to prevent, unlike C where `malloc()`/`free()` work identically for both single values and arrays. [S1]

## 🧠 핵심 개념 (Core concepts)
- **`new type`** — allocates memory for ONE value of `type`, returning a pointer to it. [S1]
- **`delete pointer;`** — releases memory allocated with plain `new`. [S1]
- **`new type[count]`** — allocates memory for an ARRAY of `count` elements; REQUIRES the bracketed form, not plain `new`. [S1]
- **`delete[] pointer;`** — releases memory allocated with `new[]`; must match the array form, not plain `delete`. [S1]
- **Memory leak** — forgetting `delete` doesn't crash the program immediately, but memory usage grows over time, eventually slowing or crashing it. [S1]
- **When to use `new`** — only when the required memory amount is unknown in advance (e.g. depends on user input) or full manual control is needed; ordinary variables never need it. [S1]

## 📖 세부 내용 (Details)
- Basic single-value allocation: `int* ptr = new int; *ptr = 35; cout << *ptr; delete ptr;`. [S1]
- Dynamic array allocation sized by runtime user input: `int numGuests; cin >> numGuests; string* guests = new string[numGuests]; for (int i = 0; i < numGuests; i++) { getline(cin, guests[i]); } delete[] guests;` — array size determined entirely at runtime, impossible with a fixed-size array. [S1]

## ⚖️ 모순 및 업데이트 (Contradictions & updates)
- **배열용 new[]/delete[]와 단일 값용 new/delete는 반드시 짝을 맞춰야 함**: C의 malloc()/free()는 단일 값과 배열 모두에 동일하게 쓰이지만, C++는 배열엔 반드시 new[]/delete[]를, 단일 값엔 new/delete를 써야 한다는 점이 명시적으로 구분됨 — 짝을 맞추지 않으면 문제가 생길 수 있음이 암시됨. [S1]

## 🛠️ 적용 사례 (Applied in summary)
호텔 투숙객 수를 사용자에게 입력받아 그만큼의 이름을 저장할 동적 배열을 만드는 예제가 원문에서 직접 실전 활용 사례로 제시됨(배열 크기를 실행 중에 결정해야 하는 상황). [S1]

## 💻 코드 패턴 (Code patterns)
A dynamic array sized at runtime by user input, freed with the matching delete[] form (C++):
```cpp
int numGuests;
cout << "How many guests? ";
cin >> numGuests;
string* guests = new string[numGuests];
cin.ignore();
for (int i = 0; i < numGuests; i++) {
  getline(cin, guests[i]);
}
delete[] guests; // must match new[] with delete[]
```

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

## 🔗 지식 그래프 (Knowledge Graph)
- **상위/루트:** [[C++ Tutorial]]
- **관련 개념:** [[CPP Memory Management]], [[CPP Functions]], [[C Memory Allocate]]
- **참조 맥락:** 참조·포인터·메모리 섹션 마지막 — 함수(Functions) 섹션으로 이어짐.

## 📚 출처 (Sources)
- [S1] W3Schools — C++ new and delete — https://www.w3schools.com/cpp/cpp_memory_management_new.asp

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