Resource acquisition is initialization: Difference between revisions

Content deleted Content added
Limitations: In the link provided, Jonathan Blow provides no evidence to back up his claim. Notably, he does not examine any alternatives to RAII (e.g. manual, arena) that might prevent such memory fragmentation in such a case where RAII would create said fragmentation.
 
(One intermediate revision by one other user not shown)
Line 9:
| access-date=2019-03-09}}</ref> is a [[programming idiom]]<ref>{{cite book |last1=Sutter |first1=Herb |author-link1=Herb Sutter |last2=Alexandrescu |first2=Andrei |author-link2=Andrei Alexandrescu |year=2005 |title=C++ Coding Standards |url=https://archive.org/details/isbn_0321113586 |url-access=limited |series=C++ In-Depth Series |publisher=Addison-Wesley |page=[https://archive.org/details/isbn_0321113586/page/n54 24] |isbn=978-0-321-11358-0 }}</ref> used in several [[Object-oriented programming|object-oriented]], [[Statically-typed programming language|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 (computer)|Resource allocation]] (or acquisition) is done during object creation (specifically initialization), by the [[Constructor (object-oriented programming)|constructor]], while resource deallocation (release) is done during object destruction (specifically finalization), by the [[Destructor (computer programming)|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 leak]]s.
 
RAII is associated most prominently with [[C++]], where it originated, but also [[Ada (programming language)|Ada]],<ref>{{cite web |title=Gem #70: The Scope Locks Idiom |url=https://www.adacore.com/gems/gem-70 |website=AdaCore |access-date=21 May 2021 |language=en}}</ref> [[Vala (programming language)|Vala]],<ref>{{cite web |author1=The Valadate Project |title=Destruction |url=https://naaando.gitbooks.io/the-vala-tutorial/content/en/4-object-oriented-programming/destruction.html |website=The Vala Tutorial version 0.30 |access-date=21 May 2021}}</ref> and [[Rust (programming language)|Rust]].<ref>{{Cite web|title=RAII - Rust By Example|url=https://doc.rust-lang.org/rust-by-example/scope/raii.html|access-date=2020-11-22|website=doc.rust-lang.org}}</ref> The technique was developed for [[Exception safety|exception-safe]] [[resource management (computing)|resource management]] in C++{{sfn|Stroustrup|1994|loc=16.5 Resource Management, pp. 388–89}} during 1984–891984–1989, primarily by [[Bjarne Stroustrup]] and [[Andrew Koenig (programmer)|Andrew Koenig]],{{sfn|Stroustrup|1994|loc=16.1 Exception Handling: Introduction, pp. 383–84}} and the term itself was coined by Stroustrup.{{sfn|Stroustrup|1994|p=389|ps=. I called this technique "resource acquisition is initialization."}}
 
Other names for this idiom include ''Constructor Acquires, Destructor Releases'' (CADRe)<ref>{{Cite web
Line 103:
 
Comparing RAII with the <code>finally</code> 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.”<ref name="faq"/>
 
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.<ref>[https://en.cppreference.com/w/cpp/language/raii.html RAII at cppreference.com]</ref>
 
==Typical uses==