Smart pointer
This post is about shared_ptr in C++11.
We will take a look at (dis)advantages of shared_ptr and how to use it. Usually we are only aware of advantages of smart pointer. Through this post, I hope we freely talk about disadvantages also.
Smart pointer?
First of all, It isn't reasonable not to talk about smart pointer. Smart pointer is what designed for preventing user from memory leak when user forgot to delete dynamic memory allocation. 'Automatic memory-care pointer' is enough to understand smart pointer. You can see advantage of smart pointer preventing mistake from below example.
Someone assign other address to normal_pointer eventhough it has already address of heap memory. If heap memory will be used after, developer should save its address to other pointer and if heap will not be used, delete operator is neeeded. But we often do these mistakes. It might be arrogant trusting oneself can care all of memory.
To see whether memroy is deleted, there are output call in destructor. Like upon, If you use smart pointer, you don't need to concern about delete memory. That means you can concentrate more on other code rather than memory.
Smart pointer is defined in <memory> header file.
unique_ptr
We will shortly learn about unique_ptr. unique_ptr is designed for caring heap memory as stack memory. Memory which is stored in stack is deleted when its containing scope ends. Like that, uique_ptr also delete memory stored in heap when scope ends.
uniqur_ptr is not different with pointer indicating stack memory. We can think it as stack memory address. Only difference is that there are no constraint when we do like 'nomal_pointer2=&stack'. But unique_ptr takes its ownership exclusively. So each of unique_ptr(s) must have different address information. It seems there are all meanings of unique in unique_ptr. Exclusive ownership, existing in only scope.
shared_ptr :: advantages
shared_ptr can share same memory which can't be done in unique_ptr. It deletes memory when no shared_ptr indicates that memory, meaning, it needs reference counting. Reference count depends on how many shared_ptr indicate that memory. When count become 0, memory will be deleted. After all, memory can be used whereever it is indicated without constraint of scope range and will be deleted when reference counting 0.
You can see destructor is called by making shared2 to nullptr. Because there are no other shared_ptr indicating that memory, memory is deleted automatically. We can refer shared_ptr as pointer that automatically delete heap memory when no one use.
shared_ptr :: How to use
There are already way to use in screenshots of codes above. We can get smart pointer caring heap memory when we put heap address as parameter. If we don't use 'new' operator or use wrong parameter, vs14 will compile code with ignore of those declaration lines. Of course, it is reasonable to make a habit of using correct parameter.
However, if we want to allocate memory to shared_ptr later, instead of using ‘new’ operator as parameter, we can use ‘make_shared()’ method. we can use it as same with ‘new’ or ‘&’ operator like in ordinary pointer.
There is another way to use shared_ptr by delivering address allocated at other lines.
shared_ptr :: disadvantages
we can use shared_ptr object as pointer due to overriding ‘*’ and ‘->’ operator. It is so convenience that smart pointer prevents memory leak by automatically deallocating heap memory. However even though shared_ptr is well designed, there are trivial disadvantages.
First is that it can’t refer stack memory.
This is code for saving object replicated by 'make_shared()' method. Duplicate constructor of Myclass is called.
shared_ptr object can’t take stack memory address directly but it can take heap address after calling duplication constructor. Although it is inconvenient that we should replicate object, it is not disadvantage also. Putting stack address to object which is designed for caring heap memory is same with ignorance for its purpose. It is also not useful to design smart pointer which can refer stack and heap. For example, If linked list is composed of not only heap but stack, It is necessary to check whether it refers dynamic memory or not when we delete node. It seems the fact that smart pointer can’t refer stack address is not a big problem.
Second disadvantage is that when several shared_ptr refer same heap memory, reference counting will increase also. Actually it is advantage as, by reference counting, smart pointer will deallocate memory at last time. But sometimes it can be problem when we want to store those memory address but want to not increase reference counting. Situation when we want to make easy of deallocating memory by maintaining counting as 1 fits this problem. In this situation, you can get function you want without increasing reference counting by directly storing heap memory address using 'get()' method of shared_ptr object or by using weak_ptr. Implementing 'move method' is another way not to increase reference counting.
Comments
Post a Comment