Resource acquisition is initialization: Difference between revisions

Content deleted Content added
Bluelink 1 book for verifiability (prndis)) #IABot (v2.0.1) (GreenC bot
This is true for satically typed langs; in the rest it's handled dynamically by interpreter/compiler
Line 6:
| last=Stroustrup
| date=2017-09-30
| accessdate=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 |chapter= |title=C++ Coding Standards |url=https://archive.org/details/isbn_0321113586 |url-access=limited |series=C++ In-Depth Series |publisher=Addison-Wesley |publication-date= |page=[https://archive.org/details/isbn_0321113586/page/n54 24] |isbn=978-0-321-11358-0 }}</ref> used in several [[Object-oriented programming language|object-oriented]], languages[[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 [[D (programming language)|D]], [[Ada (programming language)|Ada]], [[Vala (programming language)|Vala]], and [[Rust (programming language)|Rust]]. 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–89, 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."}} RAII is generally pronounced as an [[initialism]], sometimes pronounced as "R, A, double I".<ref>{{cite web