# Memory Management (Rust) > Stack กับ heap, allocator, Drop trait, RAII, การรั่วไหลของหน่วยความจำจากวงจร Rc, Pin - 20 คำถามสัมภาษณ์ - Senior - [คำถามสัมภาษณ์: Rust](https://sharpskill.dev/th/technologies/rust/interview-questions.md) ## 1. ใน Rust ตัวแปรท้องถิ่นที่เป็นชนิดพื้นฐานอย่าง i32 หรือ bool ถูกเก็บไว้ที่ใด? **คำตอบ** ตัวแปรท้องถิ่นที่เป็นชนิดพื้นฐานจะถูกเก็บไว้บน stack โดย stack ให้การจัดสรรและคืนหน่วยความจำที่รวดเร็วมากเพราะทำงานตามหลักการ LIFO (Last In, First Out) ชนิดพื้นฐานมีขนาดที่ทราบในเวลาคอมไพล์ ซึ่งทำให้คอมไพเลอร์สามารถจองพื้นที่ที่จำเป็นบน stack frame ของฟังก์ชันปัจจุบันได้ ## 2. ควรใช้ smart pointer ตัวใดในการจัดสรรค่าบน heap โดยมีเจ้าของเพียงรายเดียว? **คำตอบ** Box เป็น smart pointer ที่ง่ายที่สุดสำหรับการจัดสรรบน heap มันรับประกันการเป็นเจ้าของเพียงรายเดียวและคืนหน่วยความจำโดยอัตโนมัติเมื่อ Box ออกจาก scope Box มีประโยชน์สำหรับชนิดที่ไม่ทราบขนาดในเวลาคอมไพล์ สำหรับโครงสร้างขนาดใหญ่ที่ไม่ต้องการคัดลอกบน stack หรือสำหรับชนิดแบบเรียกซ้ำ ## 3. รูปแบบ RAII (Resource Acquisition Is Initialization) ใน Rust คืออะไร? **คำตอบ** RAII เป็นรูปแบบที่ทรัพยากร (หน่วยความจำ ไฟล์ การเชื่อมต่อ) ถูกจัดสรรในระหว่างการสร้างออบเจ็กต์และถูกปล่อยคืนโดยอัตโนมัติเมื่อออบเจ็กต์ถูกทำลาย ใน Rust สิ่งนี้ทำผ่าน trait Drop ซึ่งจะถูกเรียกโดยอัตโนมัติเมื่อค่าออกจาก scope รูปแบบนี้รับประกันว่าจะไม่มีทรัพยากรใดถูกลืมและป้องกันการรั่วไหลของหน่วยความจำ ## มีอีก 17 คำถาม - trait Drop ถูกเรียกโดยอัตโนมัติเมื่อใดใน Rust? - จะสร้างการรั่วไหลของหน่วยความจำ (memory leak) ด้วย Rc ใน Rust ได้อย่างไร? สมัครฟรี: https://sharpskill.dev/th/login ## หัวข้อสัมภาษณ์ Rust อื่นๆ - [พื้นฐาน Rust](https://sharpskill.dev/th/technologies/rust/interview-questions/rust-basics.md): 25 คำถาม, Junior - [Ownership & Borrowing](https://sharpskill.dev/th/technologies/rust/interview-questions/ownership-borrowing.md): 22 คำถาม, Junior - [Structs & Enums](https://sharpskill.dev/th/technologies/rust/interview-questions/structs-enums.md): 20 คำถาม, Junior - [การจัดการข้อผิดพลาด](https://sharpskill.dev/th/technologies/rust/interview-questions/error-handling.md): 18 คำถาม, Junior - [คอลเลกชัน](https://sharpskill.dev/th/technologies/rust/interview-questions/collections.md): 20 คำถาม, Junior - [โมดูลและแพ็กเกจ](https://sharpskill.dev/th/technologies/rust/interview-questions/modules-packages.md): 18 คำถาม, Junior - [Traits](https://sharpskill.dev/th/technologies/rust/interview-questions/traits.md): 22 คำถาม, Mid-Level - [Generics](https://sharpskill.dev/th/technologies/rust/interview-questions/generics.md): 20 คำถาม, Mid-Level - [Lifetimes](https://sharpskill.dev/th/technologies/rust/interview-questions/lifetimes.md): 20 คำถาม, Mid-Level - [Iterators & Closures](https://sharpskill.dev/th/technologies/rust/interview-questions/iterators-closures.md): 22 คำถาม, Mid-Level - [Smart Pointers](https://sharpskill.dev/th/technologies/rust/interview-questions/smart-pointers.md): 22 คำถาม, Mid-Level - [Concurrency Basics](https://sharpskill.dev/th/technologies/rust/interview-questions/concurrency-basics.md): 20 คำถาม, Mid-Level - [async/await](https://sharpskill.dev/th/technologies/rust/interview-questions/async-await.md): 22 คำถาม, Mid-Level - [Testing](https://sharpskill.dev/th/technologies/rust/interview-questions/testing.md): 18 คำถาม, Mid-Level - [Cargo & Ecosystem](https://sharpskill.dev/th/technologies/rust/interview-questions/cargo-ecosystem.md): 18 คำถาม, Mid-Level - [Pattern Matching](https://sharpskill.dev/th/technologies/rust/interview-questions/pattern-matching.md): 18 คำถาม, Mid-Level - [มาโคร](https://sharpskill.dev/th/technologies/rust/interview-questions/macros.md): 20 คำถาม, Mid-Level - [Serde และการ Serialization](https://sharpskill.dev/th/technologies/rust/interview-questions/serde-serialization.md): 20 คำถาม, Mid-Level - [Unsafe Rust](https://sharpskill.dev/th/technologies/rust/interview-questions/unsafe-rust.md): 20 คำถาม, Senior - [Trait ขั้นสูง](https://sharpskill.dev/th/technologies/rust/interview-questions/advanced-traits.md): 22 คำถาม, Senior - [Lifetime ขั้นสูง](https://sharpskill.dev/th/technologies/rust/interview-questions/advanced-lifetimes.md): 20 คำถาม, Senior - [Type System](https://sharpskill.dev/th/technologies/rust/interview-questions/type-system.md): 20 คำถาม, Senior - [Tokio & Async I/O](https://sharpskill.dev/th/technologies/rust/interview-questions/tokio-async.md): 22 คำถาม, Senior - [การเพิ่มประสิทธิภาพ](https://sharpskill.dev/th/technologies/rust/interview-questions/performance-optimization.md): 20 คำถาม, Senior - [Web Frameworks](https://sharpskill.dev/th/technologies/rust/interview-questions/web-frameworks.md): 22 คำถาม, Senior - [Database Integration](https://sharpskill.dev/th/technologies/rust/interview-questions/database-integration.md): 20 คำถาม, Senior - [Design Pattern ของ Rust](https://sharpskill.dev/th/technologies/rust/interview-questions/design-patterns.md): 20 คำถาม, Senior --- Source: SharpSkill (https://sharpskill.dev), tech interview preparation for your real stack. HTML version of this page: https://sharpskill.dev/th/technologies/rust/interview-questions/memory-management