the leak of the memory in c++ 03
2014-05-24 21:52
603 查看
The Leak of the Memory in C++
In this chaper I will introduce a new smart pointer which is scoped_ptr;
It likes auto_ptr but better. When peopel use auto_ptr, sometimes they forget
that auto_ptr alread is empty, like this;
In above situation, p1 is empty pointer after it gives pointee to p2. It
will produce some error.
But scoped_ptr can't give pointee to any pointer, so it will be better and
safer in some cases.
Now I'll introduce some special pattern to you, I say special, because in
normal cases, it just used in C++; In articles which I wrote before, I avoided
to use operator of delete, but I still use new operator, in this article I'll
introduce method which can help us to avoid use new operator.
Now let us welcome our pattern: PIMPL, it means Pointer to Implement. It
looks like below.
PersonImpl.h
Person.h
main.cpp
In above code, I did not implement Person directly, instead of a smart
pointer which point a class which implement Person. So we can use class Person
just like I use it in main function without new neither delete operators.
In this chaper I will introduce a new smart pointer which is scoped_ptr;
It likes auto_ptr but better. When peopel use auto_ptr, sometimes they forget
that auto_ptr alread is empty, like this;
auto_ptr<Person> p1(new Person); auto_ptr<Person> p2 = p1; cout << p1->name() << endl;
In above situation, p1 is empty pointer after it gives pointee to p2. It
will produce some error.
But scoped_ptr can't give pointee to any pointer, so it will be better and
safer in some cases.
Now I'll introduce some special pattern to you, I say special, because in
normal cases, it just used in C++; In articles which I wrote before, I avoided
to use operator of delete, but I still use new operator, in this article I'll
introduce method which can help us to avoid use new operator.
Now let us welcome our pattern: PIMPL, it means Pointer to Implement. It
looks like below.
PersonImpl.h
class PersonImpl { public: PersonImpl() { cout << "I'm constructed" << endl; } ~PersonImpl() { cout << "I'm destructed!" << endl; } private: int age_; string name_; public: const string& getName()const { return name_; } void setName(const string& name) { name_ = name; } int getAge() { return age_; } void setAge(int age) { age_ = age; } };
Person.h
using namespace std; using namespace boost; class Person { public: Person() : personPtr_(new PersonImpl) {} ~Person() { }; private: scoped_ptr<PersonImpl> personPtr_; public: int getAge() { return personPtr_->getAge(); } void setAge(int age) { personPtr_->setAge(age); } const string& getName()const { return personPtr_->getName(); } void setName(const string& name) { personPtr_->setName(name); } };
main.cpp
using namespace std; using namespace boost; int main(int,char**) { Person person; return 0; }
In above code, I did not implement Person directly, instead of a smart
pointer which point a class which implement Person. So we can use class Person
just like I use it in main function without new neither delete operators.
相关文章推荐
- The Leak of The Memory In C++ 1.1
- The Leak of The Memory in C++ 1.2
- A log about Reading the memroy of Other Process in C++/WIN API--ReadProcessMemory()
- The memory size of a C++ object instance.
- Question 38: What is the output of the program below in C++?
- The features of Vector in C++
- (NOT CLEAR)Question 45: Which of the following options are returned by the typeid operator in C++?
- the memory of virtual base class of C++
- Question 16: Which of the following methods can a developer use to override the default terminate() function in C++?
- 在C++中怎样获得数组的大小?(原文标题:How Would You Get the Count of an Array in C++?)
- (NOT CLEAR)Question 18: In the given C++ code snippet, which of the following statements correctly identify how Mon of enum DOW
- What is the best free memory leak detector for a C/C++ program and its plug-in DLLs
- How To Call Run() Method of the Microsoft Script Control in C++
- Question 15: Which of the following statements describe the results of executing the code snippet below in C++?
- The C in C++(Chapter 3 of Thinking in C++)
- Question 28: Which of the following statements accurately describe the new[] operator in C++?
- Question 4: Which of the following statements correctly describe the code below in C++?
- An Example about the use of map Container in C++ STL / c++ map容器的用法
- How Would You Get the Count of an Array in C++?
- What means the error-message 'java.lang.OutOfMemoryError: GC overhead limit exceeded' in Java?