Skip to content

Latest commit

 

History

History
139 lines (110 loc) · 2.81 KB

SmartPointers.md

File metadata and controls

139 lines (110 loc) · 2.81 KB

Smart Pointers in C++

Video

Some Context: new allocates memory on the Heap see details here, and delete frees up that memory.

Smart Pointers are a way to automate that process. You call new and you don't need to call delete

Essentially, it's a wrapper around that pointer.

Unique pointer

std::unique_ptr it's the weaker of them, it destroy the pointer (deallocate) when it goes out-of-scope. It cannot be copied, because if the original is freed'up, the copy will be point to garbage.

#include <string>
#include <iostream> // std::cout
#include <memory>

class Entity
{
private:
    int x;
public:
    Entity()
    {
        std::cout << "Created Entity" << std::endl;
    }

    ~Entity()
    {
        std::cout << "Destroyed Entity" << std::endl;
    }

    void Print() {}
};

int main()
{
    {
        //std::unique_ptr<Entity> entity (new Entity());
        // a more better way, slightly safer, avoid a dangling pointer
        std::unique_ptr<Entity> entity2 = std::make_unique<Entity>();

        entity2->Print();
    }
    // the entity gets Destroyed here
    std::cin.get();
}

Shared pointer

std::shared_ptr is a more "hardcore" one. Is via reference counting, it the number of references reaches 0, then it get's destroyed.

#include <string>
#include <iostream> // std::cout
#include <memory>

class Entity
{
private:
    int x;
public:
    Entity()
    {
        std::cout << "Created Entity" << std::endl;
    }

    ~Entity()
    {
        std::cout << "Destroyed Entity" << std::endl;
    }

    void Print() {}
};

int main()
{
    {
        std::shared_ptr<Entity> e0;
        {	
            std::shared_ptr<Entity> sharedEntity = std::make_shared<Entity>();
            e0 = sharedEntity;
        }
        // the entity is not destroyed here
    }
    // here the pointer is destroyed, because it has no more references to it.

    std::cin.get();
}

Weak Pointer

std::weak_ptr it works similarly to a shared pointer, but it doesn't increase our reference counter. This is great if you doesn't want to get ownership of the object.

#include <string>
#include <iostream> // std::cout
#include <memory>

class Entity
{
private:
    int x;
public:
    Entity()
    {
        std::cout << "Created Entity" << std::endl;
    }

    ~Entity()
    {
        std::cout << "Destroyed Entity" << std::endl;
    }

    void Print() {}
};

int main()
{
    {
        std::weak_ptr<Entity> e0;
        {	
            std::shared_ptr<Entity> sharedEntity = std::make_shared<Entity>();
            e0 = sharedEntity;
        }
        // the entity IS NOW destroyed here, because the weak_ptr doesn't increases the reference counter
    }	

    std::cin.get();
}