'How does unique pointer know the object is out of scope,and call the destructor automately?
i dont know why the unique pointer implement is very very easy ,but unique pointer can destruct the object when this pointer out of scope. There is anything else i dont know?
i write a test: I find when I call class's contruct fuction like unique pointer, it will call T's destruct automately. But when I call the function ,it will not call T's destruct automately. Now my question changes,i wonder why class's contruct fuction can do that ? It's RTTI? When MyPointer object destory itself?
#include<memory>
#include<iostream>
class T{
public:
T(){
std::cout<<"construct T"<<std::endl;
};
~T(){
std::cout<<"destruct T"<<std::endl;
}
};
void fun(T *t){};
template<class T>
class MyPointer
{
private:
T *__contain;
public:
MyPointer(T *t):__contain(t){
}
~MyPointer(){
delete __contain;
};
};
int main(){
using namespace std;
{
unique_ptr<T> a(new T) ;//call T' construct
unique_ptr<T> *pa=new unique_ptr<T>(new T);// dont call T' destruct
MyPointer<T> mypointer(new T);//call T' construct
T *t=new T; //dont call T' destruct
fun(new T); //dont call T' destruct
}
}
Solution 1:[1]
A unique_ptr is not much more than
template < typename T >
class unique_ptr
{
public:
unique_ptr(T* p)
:value(p)
{}
~unique_ptr()
{
delete value;
}
private:
T* value;
};
There are a lot of other methods and constructors required for a complete implementation (especially the move constructor and assignment operator) but I've omitted them as they're not relevant to your question.
When you write:
void foo()
{
unique_ptr<int> p(new int(5));
}
The new int pointer is stored in the unique_ptr object p and at the end of the foo function p is destructed which calls the unique_ptr destructor which deletes the pointed to int. Any locally allocated object will be destructed when it goes out of scope. A unique_ptr is no different it deletes the pointer it owns in its destructor. Note that if you allocate a unique_ptr with new it is the same as any other object it wont be destroyed until you call delete:
void foo()
{
unique_ptr<int>* p = new unique_ptr<int>(new int(5));
}
It's safe to say that you should pretty much never be using a dynamically allocated unique_ptr as that would defeat the purpose of using unique_ptr.
To clarify this code:
void foo()
{
unique_ptr<int> p(new int(5));
unique_ptr<int>* q = new unique_ptr<int>(new int(5));
}
Will make the compiler generate something like this:
p = stack_allocate(sizeof(unique_ptr<int>));
temp = stack_allocate(sizeof(int*));
*temp = heap_allocate(sizeof(int));
**temp = 5;
p->construct(*temp);
stack_free(temp);
q = stack_allocate(sizeof(unique_ptr<int>*));
*q = heap_allocate(sizeof(unique_ptr<int>));
temp = stack_allocate(sizeof(int*));
*temp = heap_allocate(sizeof(int));
**temp = 5;
(*q)->construct(*temp);
stack_free(temp);
stack_free(q);
p->destruct();
stack_free(p);
(In reality some of the variables are probably just held in registers not on the stack but this is generally how the generated code looks). Notice that all the stack allocated variables are automatically destroyed and deallocated whereas those created by new on the heap aren't.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
