In computer science, a smart pointer is an abstract data type that simulate a pointer while providing additonal features. -- [WikiPedia](http://en.wikipedia.org/wiki/Smart_pointer)
In the C++ language, smart pointer implements as a template class and override the behaviour of raw pointer, such as dereferencing and assignment. What's more, smart poiner also providing addtional memory managment algorithms.
From the information above, we learn that:
Here are some saying from wiki and stackoverflow:
In general, there is little difference between std and boost on smart pointer operations.
Define a class with a double variable, and just do one thing: variable num multiply itself 10,000 times.
class MyClass
{
public:
MyClass(double input);
~MyClass();
private:
double num;
};
MyClass::MyClass(double input){
num = input;
for (int i=0;i<10000;++i){
num = num * num;
}
}
The main function of raw pointer:
int main (int argc, char *argv[]){
int input = 1000;
if (argc > 1){
input = atoi(argv[1]);
}
for (int i=0;i<input;++i){
MyClass *obj = new MyClass(2.0);
}
return 1;
}
The main function of smart pointer:
int main (int argc, char *argv[]){
int input = 1000;
if (argc > 1){
input = atoi(argv[1]);
}
for (int i=0;i<input;++i){
shared_ptr<MyClass> obj(new MyClass(2.0));
}
return 1;
}
Then let input to be 10,000:
Let input to be 50,000
Let input to be 100,000
Here is the graph of performance:
From the experienments has done, we come to the conclusion that smart pointer's performance is closed to raw pointer when using in the normal way.
In order to get more details of smart pointer's performace, here is another experiment, just allocation but not calculate:
A new class:
Class MyClass{
public:
MyClass(double input);
~MyClass();
void print();
private:
double num;
};
MyClass::MyClass(double input){
num = 2;
for (int i=0;i<input;i++){
num = num * num;
}
}
MyClass::~MyClass(){ }
void MyClass::print(){
cout << num << " ";
}
The main function of smart pointer:
int main(int argc, char *argv[]){
int input = 1;
if (argc > 1){
input = atoi(argv[1]);
}
shared_ptr<MyClass> obj(new MyClass(input));
return 1;
}
The main function of raw pointer:
int main(int argc, char *argv[]){
int input = 1;
if (argc > 1){
input = atoi(argv[1]);
}
MyClass* obj = new MyClass(input);
delete obj;
return 1;
}
And the result:
Conclusion: Compared to raw pointer, smart pointer spends more time on allocating.
First of all, #include
template<class Y>
explicit shared_ptr(Y* ptr);
The key word explicit avoids compiler to construct object using implicit conversion. For example:
shared_ptr<MyClass> obj = new MyClass();
In the case without using explicit key word: obj will convert to shared_ptr type.
shared_ptr<MyClass> obj(new MyClass(2.0));
vector<shared_ptr<CDNA> > data;
Please notice we use "> >", not ">>". ">>" refers to stream operator.
obj.reset();
obj->print();
obj.get(index);
swap(obj1, obj2);
http://www.codesynthesis.com/~boris/blog/2010/05/24/smart-pointers-in-boost-tr1-cxx-x0/ http://pic.dhe.ibm.com/infocenter/aix/v6r1/index.jsp?topic=%2Fcom.ibm.aix.cmds%2Fdoc%2Faixcmds5%2Ftime.htm http://gcc.gnu.org/onlinedocs/gcc-4.6.0/libstdc++/api/a01033_source.html http://www.boost.org/doc/libs/1_53_0/libs/smart_ptr/smart_ptr.htm http://stackoverflow.com/questions/4902313/difference-between-boostshared-ptr-and-stdshared-ptr-from-the-standard-memo http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3225.pdf http://en.wikipedia.org/wiki/C%2B%2B0x#General-purpose_smart_pointers http://en.wikipedia.org/wiki/Smart_pointer#C.2B.2B_smart_pointers http://en.cppreference.com/w/cpp/memory/shared_ptr