大姨妈来了不能吃什么东西| 娃娃脸适合什么发型| 看月经挂什么科| 止疼药吃多了有什么副作用| 藏红花有什么功效| 腿肿挂什么科| 什么方法可以快速排便| 乙木的根是什么| 龙虾不能和什么一起吃| 低钾是什么原因引起的| 肠痈是什么病| 大姨夫是什么意思| 小便短赤是什么症状| 考研都考什么科目| 女生下体长什么样子| 来例假腰疼是什么原因| 中国最长的河是什么河| 71属什么生肖| 肚子疼吃什么| 女人做梦哭醒预示什么| 胸部彩超能检查出什么| 巧克力囊肿有什么症状表现| 冬至要注意什么| 5月9号什么星座| 腿浮肿是什么原因| 打封闭是什么意思| 子母被是什么意思| 宋朝之后是什么朝代| 孕妇梦见洪水是什么意思| 7.13是什么日子| 蒙脱石散什么时候吃| 帕罗西汀是什么药| 转氨酶高是什么问题| 2017属什么生肖| 网球肘吃什么药| 小孩长得慢是什么原因| 蛇缠腰是什么病怎么治| 肺结节吃什么好| 什么王| 尿毒症是什么原因引起的| 社保跟医保有什么区别| 吃饭肚子疼是什么原因| 黄酒有什么功效与作用| 白细胞酯酶阳性是什么意思| 龙头凤尾是什么生肖| 鞠婧祎什么星座| 大校上面是什么军衔| 什么药可以缩阴紧致| 天秤座跟什么星座最配| 蜜蜡属于什么五行属性| 杏仁有什么作用和功效| 什么瓜不能吃脑筋急转弯| 右侧后背疼是什么原因| 红豆为什么代表相思| 减肥晚餐吃什么好| 老而弥坚是什么意思| 推头是什么意思| 从来不吃窝边草是什么生肖| 什么情况需要打破伤风| 洗面奶和洁面乳有什么区别| 喉咙痒干咳吃什么药| 胃胀气吃什么药| bid是什么意思| 感叹是什么意思| 抖阴是什么| 五位一体是什么| poscer是什么牌子手表| 上眼皮痒是什么原因| 2段和3段奶粉有什么区别| 什么人容易得脑溢血| 手起皮是什么原因| 头疼发烧吃什么药| 气郁症是什么症状| 婴儿喝什么奶粉最好| 感叹号像什么| 硫酸羟氯喹片治什么病| 伤口好转的迹象是什么| 脚底有黑痣有什么说法| 蚊子为什么吸血| 很能睡觉是什么原因| 丝瓜有什么好处| 清蒸鱼一般用什么鱼| sey什么意思| 怀孕呕吐吃什么可以缓解| hp检查是什么| 冲蛇煞西是什么意思| 星期一左眼皮跳是什么预兆| 达芬奇是干什么的| 2020是什么生肖| 天乙贵人什么意思| 1988是什么年| 复合维生素b片主治什么病| 周年祭日有什么讲究| 闰月要给父母买什么| 心脾两虚吃什么药| 什么牌子皮带结实耐用| 黄鼠狼为什么叫黄大仙| 口腔溃疡看什么科| 一直流鼻血是什么原因| 未退化胸腺是什么意思| 羊水穿刺主要检查什么| 月字旁有什么字| er是什么元素| 什么什么一窝| 男子精少吃什么药可以生精| 告别是什么意思| 茅根是什么| hc是什么意思| 什么病可以鉴定病残| 宝宝嘴巴臭臭的是什么原因| 八字缺什么怎么算| porsche是什么牌子的车| 芝士是什么东西| 劝酒什么意思| 左腿酸痛是什么原因| 笃定什么意思| 12.28是什么星座| 做完胃镜可以吃什么| 八仙桌是什么生肖| 灰指甲什么症状| 嘴里甜是什么原因| 梦见枪毙人是什么意思| 猫代表什么象征意义| 三个吉念什么| 肚脐周围是什么肠| 先自度其足的度是什么意思| 头发有点黄是什么原因| 宝宝拉肚子吃什么| 嬉皮士是什么意思| 布洛芬0.3和0.4g有什么区别| 吃什么可以消除淋巴结| 古丽是什么意思| 96属什么生肖| 怀孕都有什么症状| 63年的兔是什么命| h是什么牌子| 人为什么会发烧| 鲱鱼是什么鱼| 什么淀粉最好| 11月12日是什么星座| 风湿性心脏病是什么原因引起的| 心语是什么意思| 什么的东西| 白粉病用什么药| 七月六号是什么星座| 真菌感染脚气用什么药| 溥仪为什么没有后代| 情节是什么| 桃李满天下什么意思| 贾宝玉大名叫什么| 吃brunch是什么意思啊| 眼底出血用什么眼药水| 白玫瑰的花语是什么| 多维元素片有什么作用| 口扫是什么| 什么的诉说| 唇炎抹什么药膏最有效| 亲家母是什么意思| 吃什么助眠| 打完疫苗不能吃什么| 高密度脂蛋白胆固醇低是什么意思| pa是什么单位| 什么星球最大| 生吃苦瓜有什么好处和坏处| 风什么意思| 诸葛亮长什么样| 吃斋是什么意思| 为什么感觉| 灵魂摆渡人是什么意思| 什么是白肺| geforce是什么牌子| 肝风内动是什么原因造成的| 什么的肩膀| 风寒感冒吃什么药效果好| khaki是什么颜色| 鱼扣是鱼的什么部位| 电风扇什么牌子好| 腿部抽筋是什么原因引起的| 赤潮是什么| 睾丸皮痒用什么药膏| 雌二醇低吃什么补得快| 离婚证是什么颜色| 1月份是什么星座| 龙虾不能和什么一起吃| 口腔溃疡挂什么科室| 打卡什么意思| 口腔溃疡吃什么中成药| 03年属什么| 鼻毛变白什么征兆| 胃胀什么原因| 什么是玄关在哪个位置| 森林里有什么| 做梦梦到鱼是什么意思| 膝盖内侧疼是什么原因| 有气质是什么意思| 喜欢白色的人是什么性格| 儿童弱视是什么原因引起的| 小孩尿酸高是什么原因| 乳腺靶向检查是什么| 天仙是什么意思| 助听器什么品牌最好| 双抗是什么药| 南京区委书记什么级别| 什么是嘌呤| 胰腺炎吃什么中成药| 黄芪和什么搭配最好| 什么叫老人临终骨折| 猫咪取什么名字好听| 小孩睡觉磨牙是什么原因| 两融是什么意思| 金银花搭配什么泡水喝好| 什么肥什么壮| 什么时候做nt| 以讹传讹什么意思| ecc是检查什么的| 降钙素原是什么意思| 晚上2点是什么时辰| 古稀是什么意思| 愿力是什么意思| 睡前吃什么有助于睡眠| hr是什么品牌| 九三年属鸡是什么命| jay什么意思| 最熟悉的陌生人是什么意思| 谁也不知道下一秒会发生什么| 农历2月12日是什么星座| 右肺中叶小结节是什么意思严重吗| 奇点是什么意思| 妈宝女是什么意思| 运气是什么意思| 青花鱼是什么鱼| 什么是感恩| 孕妇为什么要躲着白事| 盼头是什么意思| 什么是十字花科蔬菜| 什么的辨认| 庄子是什么学派| 强扭的瓜不甜什么意思| 女性前列腺叫什么| 喵喵喵是什么意思| 汗血宝马什么意思| 桑叶泡水喝有什么功效和作用| 平均红细胞体积偏高是什么原因| 曹操是个什么样的人| 吃茶油对身体有什么好处| 冥冥中是什么意思| av是什么| 斯德哥尔摩综合症是什么| 脾肾阳虚吃什么药最好| 鬓角长痘痘是什么原因| 英语什么时候学最好| ebay什么意思| 济公是什么生肖| 71年属猪是什么命| 男命正官代表什么| 拉肚子呕吐吃什么药| 什么火锅最好吃| 机场地勤是干什么的| 才能是什么意思| 阴茎里面痒是什么原因| 干咳吃什么药最有效| 布病是什么| 扁桃体炎吃什么药最好效果好| 小孩为什么会流鼻血| 百度Jump to content

【赠票】《春娇救志明》这对CP婚后搞咩嘢新名堂?

From Wikipedia, the free encyclopedia
百度 智研咨询发布的《20172023年中国光伏产业深度调研与发展趋势分析报告》分析称,太阳能十三五新增装机目标是6681万千瓦,2016年我国光伏发电新增装机容量已完成3454万千瓦,预计2017年光伏发电新增装机在1100万千瓦左右。

Resource acquisition is initialization (RAII)[1] is a programming idiom[2] used in several object-oriented, statically typed programming languages to describe a particular language behavior. In RAII, holding a resource is a class invariant, and is tied to object lifetime. Resource allocation (or acquisition) is done during object creation (specifically initialization), by the constructor, while resource deallocation (release) is done during object destruction (specifically finalization), by the destructor. In other words, resource acquisition must succeed for initialization to succeed. Thus, the resource is guaranteed to be held between when initialization finishes and finalization starts (holding the resources is a class invariant), and to be held only when the object is alive. Thus, if there are no object leaks, there are no resource leaks.

RAII is associated most prominently with C++, where it originated, but also Ada,[3] Vala,[4] and Rust.[5] The technique was developed for exception-safe resource management in C++[6] during 1984–1989, primarily by Bjarne Stroustrup and Andrew Koenig,[7] and the term itself was coined by Stroustrup.[8]

Other names for this idiom include Constructor Acquires, Destructor Releases (CADRe)[9] and one particular style of use is called Scope-based Resource Management (SBRM).[10] This latter term is for the special case of automatic variables. RAII ties resources to object lifetime, which may not coincide with entry and exit of a scope. (Notably variables allocated on the free store have lifetimes unrelated to any given scope.) However, using RAII for automatic variables (SBRM) is the most common use case.

C++11 example

[edit]

The following C++11 example demonstrates usage of RAII for file access and mutex locking:

#include <fstream>
#include <iostream>
#include <mutex>
#include <stdexcept>
#include <string>

void WriteToFile(const std::string& message) {
  // |mutex| is to protect access to |file| (which is shared across threads).
  static std::mutex mutex;

  // Lock |mutex| before accessing |file|.
  std::lock_guard<std::mutex> lock(mutex);

  // Try to open file.
  std::ofstream file("example.txt");
  if (!file.is_open()) {
    throw std::runtime_error("unable to open file");
  }

  // Write |message| to |file|.
  file << message << std::endl;

  // |file| will be closed first when leaving scope (regardless of exception)
  // |mutex| will be unlocked second (from |lock| destructor) when leaving scope
  // (regardless of exception).
}

This code is exception-safe because C++ guarantees that all objects with automatic storage duration (local variables) are destroyed at the end of the enclosing scope in the reverse order of their construction.[11] The destructors of both the lock and file objects are therefore guaranteed to be called when returning from the function, whether an exception has been thrown or not.[12]

Local variables allow easy management of multiple resources within a single function: they are destroyed in the reverse order of their construction, and an object is destroyed only if fully constructed—that is, if no exception propagates from its constructor.[13]

Using RAII greatly simplifies resource management, reduces overall code size and helps ensure program correctness. RAII is therefore recommended by industry-standard guidelines,[14] and most of the C++ standard library follows the idiom.[15]

Benefits

[edit]

The advantages of RAII as a resource management technique are that it provides encapsulation, exception safety (for stack resources), and locality (it allows acquisition and release logic to be written next to each other).

Encapsulation is provided because resource management logic is defined once in the class, not at each call site. Exception safety is provided for stack resources (resources that are released in the same scope as they are acquired) by tying the resource to the lifetime of a stack variable (a local variable declared in a given scope): if an exception is thrown, and proper exception handling is in place, the only code that will be executed when exiting the current scope are the destructors of objects declared in that scope. Finally, locality of definition is provided by writing the constructor and destructor definitions next to each other in the class definition.

Resource management therefore needs to be tied to the lifespan of suitable objects in order to gain automatic allocation and reclamation. Resources 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.

Comparing RAII with the finally construct used in Java, Stroustrup wrote that “In realistic systems, there are far more resource acquisitions than kinds of resources, so the 'resource acquisition is initialization' technique leads to less code than use of a 'finally' construct.”[1]

As a class invariant, RAII provides guarantees that an object instance that is supposed to have acquired a resource has in fact done so. This eliminates the need for additional "setup" methods to get a newly-created object into a usable state (all such work is performed in the constructor; similarly, "shutdown" tasks to release resources occur in the object's destructor), and the need to test instances to verify that they have been properly set up before every use.[16]

Typical uses

[edit]

The RAII design is often used for controlling mutex locks in multi-threaded applications. In that use, the object releases the lock when destroyed. Without RAII in this scenario the potential for deadlock would be high and the logic to lock the mutex would be far from the logic to unlock it. With RAII, the code that locks the mutex essentially includes the logic that the lock will be released when execution leaves the scope of the RAII object.

Another typical example is interacting with files: We could have an object that represents a file that is open for writing, wherein the file is opened in the constructor and closed when execution leaves the object's scope. In both cases, RAII ensures only that the resource in question is released appropriately; care must still be taken to maintain exception safety. If the code modifying the data structure or file is not exception-safe, the mutex could be unlocked or the file closed with the data structure or file corrupted.

Ownership of dynamically allocated objects (memory allocated with new in C++) can also be controlled with RAII, such that the object is released when the RAII (stack-based) object is destroyed. For this purpose, the C++11 standard library defines the smart pointer classes std::unique_ptr for single-owned objects and std::shared_ptr for objects with shared ownership. Similar classes are also available through std::auto_ptr in C++98, and boost::shared_ptr in the Boost libraries.

Also, messages can be sent to network resources using RAII. In this case, the RAII object would send a message to a socket at the end of the constructor, when its initialization is completed. It would also send a message at the beginning of the destructor, when the object is about to be destroyed. Such a construct might be used in a client object to establish a connection with a server running in another process.

Compiler "cleanup" extensions

[edit]

Both Clang and the GNU Compiler Collection implement a non-standard extension to the C language to support RAII: the "cleanup" variable attribute.[17] The following annotates a variable with a given destructor function that it will call when the variable goes out of scope:

void example_usage() {
  __attribute__((cleanup(fclosep))) FILE *logfile = fopen("logfile.txt", "w+");
  fputs("hello logfile!", logfile);
}

In this example, the compiler arranges for the fclosep function to be called on logfile before example_usage returns.

Limitations

[edit]

RAII only works for resources acquired and released (directly or indirectly) by stack-allocated objects, where there is a well-defined static object lifetime. Heap-allocated objects which themselves acquire and release resources are common in many languages, including C++. RAII depends on heap-based objects to be implicitly or explicitly deleted along all possible execution paths, in order to trigger its resource-releasing destructor (or equivalent).[18]:?8:27? This can be achieved by using smart pointers to manage all heap objects, with weak pointers for cyclically referenced objects.

In C++, stack unwinding is only guaranteed to occur if the exception is caught somewhere. This is because "If no matching handler is found in a program, the function terminate() is called; whether or not the stack is unwound before this call to terminate() is implementation-defined (15.5.1)." (C++03 standard, §15.3/9).[19] This behavior is usually acceptable, since the operating system releases remaining resources like memory, files, sockets, etc. at program termination.[citation needed]

At the 2018 Gamelab conference, Jonathan Blow claimed that use of RAII can cause memory fragmentation which in turn can cause cache misses and a 100 times or worse hit on performance.[20]

Reference counting

[edit]

Perl, Python (in the CPython implementation),[21] and PHP[22] manage object lifetime by reference counting, which makes it possible to use RAII. Objects that are no longer referenced are immediately destroyed or finalized and released, so a destructor or finalizer can release the resource at that time. However, it is not always idiomatic in such languages, and is specifically discouraged in Python (in favor of context managers and finalizers from the weakref package).[citation needed]

However, object lifetimes are not necessarily bound to any scope, and objects may be destroyed non-deterministically or not at all. This makes it possible to accidentally leak resources that should have been released at the end of some scope. Objects stored in a static variable (notably a global variable) may not be finalized when the program terminates, so their resources are not released; CPython makes no guarantee of finalizing such objects, for instance. Further, objects with circular references will not be collected by a simple reference counter, and will live indeterminately long; even if collected (by more sophisticated garbage collection), destruction time and destruction order will be non-deterministic. In CPython there is a cycle detector which detects cycles and finalizes the objects in the cycle, though prior to CPython 3.4, cycles are not collected if any object in the cycle has a finalizer.[23]

References

[edit]
  1. ^ a b Stroustrup, Bjarne (2025-08-05). "Why doesn't C++ provide a "finally" construct?". Retrieved 2025-08-05.
  2. ^ Sutter, Herb; Alexandrescu, Andrei (2005). C++ Coding Standards. C++ In-Depth Series. Addison-Wesley. p. 24. ISBN 978-0-321-11358-0.
  3. ^ "Gem #70: The Scope Locks Idiom". AdaCore. Retrieved 21 May 2021.
  4. ^ The Valadate Project. "Destruction". The Vala Tutorial version 0.30. Retrieved 21 May 2021.
  5. ^ "RAII - Rust By Example". doc.rust-lang.org. Retrieved 2025-08-05.
  6. ^ Stroustrup 1994, 16.5 Resource Management, pp. 388–89.
  7. ^ Stroustrup 1994, 16.1 Exception Handling: Introduction, pp. 383–84.
  8. ^ Stroustrup 1994, p. 389. I called this technique "resource acquisition is initialization."
  9. ^ Arthur Tchaikovsky (2025-08-05). "Change official RAII to CADRe". ISO C++ Standard - Future Proposals. Google Groups. Retrieved 2025-08-05.
  10. ^ Chou, Allen (2025-08-05). "Scope-Based Resource Management (RAII)". Retrieved 2025-08-05.
  11. ^ Richard Smith (2025-08-05). "Working Draft, Standard for Programming Language C++" (PDF). p. 151, section §9.6. Retrieved 2025-08-05.
  12. ^ "How can I handle a destructor that fails?". Standard C++ Foundation. Retrieved 2025-08-05.
  13. ^ Richard Smith (2025-08-05). "Working Draft, Standard for Programming Language C++" (PDF). Retrieved 2025-08-05.
  14. ^ Stroustrup, Bjarne; Sutter, Herb (2025-08-05). "C++ Core Guidelines". Retrieved 2025-08-05.
  15. ^ "I have too many try blocks; what can I do about it?". Standard C++ Foundation. Retrieved 2025-08-05.
  16. ^ RAII at cppreference.com
  17. ^ "Specifying Attributes of Variables". Using the GNU Compiler Collection (GCC). GNU Project. Retrieved 2025-08-05.
  18. ^ Weimer, Westley; Necula, George C. (2008). "Exceptional Situations and Program Reliability" (PDF). ACM Transactions on Programming Languages and Systems. Vol. 30, no. 2.
  19. ^ ildjarn (2025-08-05). "RAII and Stack unwinding". Stack Overflow. Retrieved 2025-08-05.
  20. ^ Gamelab2018 - Jon Blow's Design decisions on creating Jai a new language for game programmers on YouTube
  21. ^ "Extending Python with C or C++: Reference Counts". Extending and Embedding the Python Interpreter. Python Software Foundation. Retrieved 2025-08-05.
  22. ^ hobbs (2025-08-05). "Does PHP support the RAII pattern? How?". Retrieved 2025-08-05.
  23. ^ "gc — Garbage Collector interface". The Python Standard Library. Python Software Foundation. Retrieved 2025-08-05.

Further reading

[edit]
[edit]
菠萝蜜不能和什么一起吃 取环后月经量少是什么原因 汗多尿少是什么原因 铜绿假单胞菌用什么抗生素 月经血是黑色的是什么原因
米糠是什么东西 菊花茶和枸杞一起泡水有什么好处 健脾祛湿吃什么药 夜盲吃什么维生素 维生素d3什么牌子好
过敏性皮肤用什么护肤品比较好 射手座是什么星象 生物碱是什么 万圣节应该送什么礼物 后脑勺发热是什么原因
肛门疼痛什么原因 什么肠小道成语 病毒性疣是什么病 工段长是什么级别 明心见性是什么意思
血尿挂什么科hcv7jop7ns4r.cn 命是什么意思hcv8jop2ns7r.cn 诗五行属性是什么hcv9jop3ns0r.cn 田七配什么煲汤最好hcv8jop8ns9r.cn x片和ct有什么区别hebeidezhi.com
洗葡萄用什么洗最干净hcv7jop6ns5r.cn outlets是什么意思hcv7jop6ns5r.cn 淋巴细胞百分比高是什么原因hcv8jop2ns5r.cn 潮宏基是什么档次的hcv7jop4ns6r.cn 下午两点属于什么时辰hcv9jop4ns9r.cn
什么食物补气hcv7jop5ns2r.cn 风热感冒用什么药好hcv9jop3ns0r.cn 支气管炎挂什么科wuhaiwuya.com 夜间睡觉出汗是什么原因hcv8jop5ns7r.cn 台阶是什么意思hcv9jop1ns5r.cn
中央候补委员是什么级别hcv8jop6ns4r.cn opv是什么疫苗hcv7jop7ns4r.cn 多指是什么遗传方式hcv9jop6ns0r.cn 病灶什么意思hcv7jop5ns0r.cn 迪桑特属于什么档次hcv8jop2ns0r.cn
百度