RAIIとは何か?
RAII(resource acquisition is initialization)は、メモリ、ファイル、ロックなどのリソースをオブジェクトのコンストラクタで取得し、デストラクタで解放するパターンです。オブジェクトがスコープを外れると、クリーンアップが自動的かつ決定論的に実行されます。これにより、リソース管理を手動の解放ではなく通常のスコープ規則に結び付けます。
なぜ重要か
RAIIはクリーンアップをスコープの終了に結び付けるため、例外の発生時も含めてリークやダブルフリーの可能性を大幅に減らします。garbage collectionを持たない言語における決定論的なリソース管理の基盤です。
関連ガイド
What Are Move Semantics?Move semantics transfer ownership of a resource from one object to another without copying, leaving the sourc…
What Is Reference Counting?Reference counting tracks how many references point to an object and frees it automatically when that count d…
What Is a Stack Frame?A stack frame is the block of stack memory for one function call, holding its arguments, local variables, and…