码迷,mamicode.com
首页 > 其他好文 > 详细

RAII

时间:2018-01-10 11:28:02      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:mit   oca   nbsp   sig   div   stack   red   lan   min   

Resource Acquisition Is Initialization, often referred to by the acronym RAII (or, erroneously, RIIA), is a popular design pattern in several object oriented programming languages like C++, D and Ada. The technique was invented by Bjarne Stroustrup,[1] to deal with resource deallocation in C++. In this language, the only code that can be guaranteed to be executed after an exception is thrown are the destructors of objects residing on the stack. Resources therefore need to be tied to the lifespan of suitable objects. They are acquired during initialization, when there is no chance of them being used before they are available, and released with the destruction of the same objects, which is guaranteed to take place even in case of errors.
RAII is vital in writing exception-safe C++ code: to release resources before permitting exceptions to propagate (in order to avoid resource leaks) one can write appropriate destructors once rather than dispersing and duplicating cleanup logic between exception handling blocks that may or may not be executed.
http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization

 

1. used for apply/release semaphore

2. used for apply/release memory in hop

3. do {} while(0)

RAII

标签:mit   oca   nbsp   sig   div   stack   red   lan   min   

原文地址:https://www.cnblogs.com/atoman/p/8256939.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!