|
INTERVIEW QUESTIONS
C++
ARRAYS, STRINGS, POINTERS AND REFERENCES IN C++
DETAILS
Question: What happens to the member pointers when an exception occurs in constructor, while allocating memory?How can we over come this ?
Answer: Offcourse it will result memory leak.... so use auto_ptr for such thing .. see following example, i have used two types to avoid memory leaks one is auto_ptr and another is initialize function
#include <iostream>
#include <memory>
class DataClass{
public:
int m_iValue;
DataClass(int i =0):m_iValue(i){}
void show(void) {
std::cout<<std::endl<<"Value :- "<<m_iValue<<std::endl;
}
};
class ResourceLeakFreeClass{
public:
std::auto_ptr<DataClass> m_Data1;
DataClass * m_Data2;
ResourceLeakFreeClass():m_Data1(new DataClass(100)),m_Data2(InitDataClass(200)){}
~ResourceLeakFreeClass(){
if(m_Data2){
delete m_Data2;
m_Data2 = 0;
}
}
DataClass * InitDataClass(int iData){
try{return new DataClass(iData);}
catch (...) {delete m_Data2;throw;}
}
};
int main(int argc, char* argv[])
{
ResourceLeakFreeClass RLFC;
RLFC.m_Data1->show();
RLFC.m_Data2->show();
return 0;
}
|
|
|
Category |
Arrays, Strings, Pointers and References in C++ Interview Questions & Answers -
Exam Mode /
Learning Mode
|
Rating |
(0.2) By 8231 users |
Added on |
8/18/2011 |
Views |
74314 |
Rate it! |
|
|
Question:
What happens to the member pointers when an exception occurs in constructor, while allocating memory?How can we over come this ?
Answer:
Offcourse it will result memory leak.... so use auto_ptr for such thing .. see following example, i have used two types to avoid memory leaks one is auto_ptr and another is initialize function
#include <iostream>
#include <memory>
class DataClass{
public:
int m_iValue;
DataClass(int i =0):m_iValue(i){}
void show(void) {
std::cout<<std::endl<<"Value :- "<<m_iValue<<std::endl;
}
};
class ResourceLeakFreeClass{
public:
std::auto_ptr<DataClass> m_Data1;
DataClass * m_Data2;
ResourceLeakFreeClass():m_Data1(new DataClass(100)),m_Data2(InitDataClass(200)){}
~ResourceLeakFreeClass(){
if(m_Data2){
delete m_Data2;
m_Data2 = 0;
}
}
DataClass * InitDataClass(int iData){
try{return new DataClass(iData);}
catch (...) {delete m_Data2;throw;}
}
};
int main(int argc, char* argv[])
{
ResourceLeakFreeClass RLFC;
RLFC.m_Data1->show();
RLFC.m_Data2->show();
return 0;
} Source: CoolInterview.com
Constructor can't throw Exceptions. Source: CoolInterview.com
Answered by: jitendra singh | Date: 1/26/2008
| Contact jitendra singh
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.
|
Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.
View All Arrays, Strings, Pointers and References in C++ Interview Questions & Answers - Exam Mode /
Learning Mode
|