Title: Sushi on the fish market in Tokyo, Source: own resources, Authors: Agnieszka and Michał Komorowscy
In my previous post I wrote that C++ doesn't have the automatic garbage collection. However, it's not entirely true. Let's think about it more carefully. To start, look at the following method in C#. It simply creates an instance of MyObject class.
public void Fun() { var x = new MyObject(); //... }When this object will be collected by GC? When it is needed but not sooner than the execution of Fun method is finished. Can we do the same thing in C++? Of course yes, here is an example:
void fun() { MyObject x; //... }If you are not familiar with C++ you may say that I only declared the variable x in this example and that I didn't create any object. Well, in C++ the following 3 commands are equivalent:
MyObject x;
MyObject x();
MyObject x = MyObject();
Personally, I still cannot used to that ;) But let's return to C++ and ask the similar question as for C#. When a destructor will be called for created instance of MyObject? The answer is easy - when the execution of fun method is over. Or in other words when an object goes out of scope. It's worth nothing that this behaviour is called the automatic storage duration. Usually it is implemented by using stack however it's not the rule. Now, let's consider this code in C++:
void fun() { MyObject* x = new MyObject(); //... }It looks almost the same like in C#. However, this time we're creating an object dynamically with new keyword. And this kind of objects won't be removed automatically from the memory, even if the execution of fun is over. This is also known as the dynamic storage duration. How to release this kind of objects?
In the past a C++ developer had to use delete keyword to do so. I did the same in the code from my post about virtual destructors. However, since C++ 11 we can use something else i.e. the explicit automatic garbage collection. More precisely I'm talking about smart pointers. Here is the rewritten version of Node class:
class Node { public: static int _count; Node(int i) : _value(i) { Node::_count++; } ~Node() { std::cout << "~Node " << _value; _count--; } int _value; std::unique_ptr<Node> _left = nullptr; std::unique_ptr<Node> _right = nullptr; }; int Node::_count = 0;In this code I defined ~Node destructor only for its side effects i.e. to decrease a counter. I didn't use delete in this code at all. Instead I wrapped pointers with std::unique_ptr. It works in this way that it releases the pointer when it goes out of scope. Nothing more nothing less. But thanks to that we don't have to remember about delete. Almost like in C#. Here is the testing code:
int main() { Node* root = new Node(1); root->_left = std::unique_ptr<Node>(new Node(2)); root->_right = std::unique_ptr<Node>(new Node(3)); std::cout << " Existing nodes: " << Node::_count; delete root; std::cout << " Existing nodes: " << Node::_count; }I didn't wrap root in a smart pointer because I wanted to explicitly use delete and verify the final number of nodes. Easy, isn't it?
At the end it's worth mentioning that there are also different types of smart pointers. shared_ptr should be used when the same pointer can have many owners. Whereas std::weak_ptr represents the same concept as C# WeakReference class. Last but not least, except the automatic storage duration and the dynamic storage duration we also have the static storage duration and the thread storage duration. The former is used to store static variables which are release at the end of program (pretty the same as in C#) and the latter to store variables that will survive until the end of a thread (in C# we can use TheradLocalStorage for the similar effect). More reading can be found here.
2 comments:
Hi,
thanks for writing the "C++ for C# developers" series. I really like it.
I think I found two bugs:
1) I think you mean C++ 11 and not C# 11.
2) I think that in line "MyObject x = new MyObject();" x has to be a pointer.
Hi, It's good to hear that this series is useful for someone. Thanks for your comments. I've already updated the post.
Post a Comment