Let's look at shared_ptr's applications and problems we are often faced with. There are beautiful examples in data structure such as linked-list and tree. In this post, we will take tree example. Before learning shared_ptr, it is common to use normal pointer to save children node's information. When taking tree structure, we can use shared_ptr instead of normal pointer since every node is usually allocated in heap memory. Advantage, when using shared_ptr, is ofcurse automatic memory releasing. In existing tree structure, Node class should be deleted from high level(or leaf node). So we create wrapping class called 'tree' and use postorder algorithm to release memory. However Node class ,used with shared_ptr, doesn't need wrapper one and also dose not need to do anything in destructor. Memory will be released automatically in an ordered way. For example, when node is deleted, deleting shared_ptr also follows. Then, shared_ptr's de...
Comments
Post a Comment