Question: Can you explain the term "resource acquisition is initialization?"
Answer: Resource Acquisition is Initialisation or shortly RAII is a term closely connected with exception handling and automatic garbage collection(speaking loosely). RAII is defined as the technique of performing all necessary resource initialisation work right within the body of the constructor itself, and correspondingly doing all the deallocation within the destructor. Since during exception handling destructors of all the classes whose constructors have been successfully invoked, are called therefore all the local objects previously allocated are automatically deallocated without explicitly calling their destructors.the following example explains the RAII technique:class X{ int *r; public: X(){cout<<"X is created"; r=new int[10]; } ~X(){cout<<"X is destroyed"; delete [] r; }};class Y{ public: Y(){ X x; throw 44; } ~Y(){cout<<"Y is destroyed";}};Now since Y throws an exception right in its constructor therefore its destructor won't get called becuse the constructor was not invoked successfully. But the destructor of the class X will definitely be called and therefore the array 'r' will be deallocated. So we didn't have to care about resource deallocation at the time exceptions are raised.
Question:
Can you explain the term "resource acquisition is initialization?"
Answer:
Resource Acquisition is Initialisation or shortly RAII is a term closely connected with exception handling and automatic garbage collection(speaking loosely). RAII is defined as the technique of performing all necessary resource initialisation work right within the body of the constructor itself, and correspondingly doing all the deallocation within the destructor. Since during exception handling destructors of all the classes whose constructors have been successfully invoked, are called therefore all the local objects previously allocated are automatically deallocated without explicitly calling their destructors.the following example explains the RAII technique:class X{ int *r; public: X(){cout<<"X is created"; r=new int[10]; } ~X(){cout<<"X is destroyed"; delete [] r; }};class Y{ public: Y(){ X x; throw 44; } ~Y(){cout<<"Y is destroyed";}};Now since Y throws an exception right in its constructor therefore its destructor won't get called becuse the constructor was not invoked successfully. But the destructor of the class X will definitely be called and therefore the array 'r' will be deallocated. So we didn't have to care about resource deallocation at the time exceptions are raised. Source: CoolInterview.com
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
There should not be any Spelling Mistakes.
There should not be any Gramatical Errors.
Answers must not contain any bad words.
Answers should not be the repeat of same answer, already approved.
Answer should be complete in itself.
Related Questions
View Answer
What is the difference between operator new and the new operator?