Monday, June 2 2025

Today's music
今日の音楽
How do smart pointers actually used? Let's take a look at this simple code:

// Global declaration
Enemy enemy;

void Update()
{
    enemy.Update();
}
When this happens, data will be allocated on the stack immediately, even if the enemy hasn't spawned yet.
We don't want that. So how do we fix it to only spawn dynamically when needed?
The trick is to use heap allocation. In heap, memory is temporary and manageable.

std::shared_ptr<Enemy> enemy = std::make_shared<Enemy>();
enemy->Update();
With this, the enemy container exists dynamically in heap memory.
"std::shared_ptr<Type>" means: it's empty at first, and only stays alive if someone owns it.

// Instantly removed from memory when no longer referenced
enemy = nullptr;
Keep in mind that std::shared_ptr<Enemy> enemy = std::make_shared<Enemy>();
must stay in scope. When it goes out of scope, it’s deleted automatically.
Raw pointers do not behave this way.

// Raw pointer example
Enemy* enemy = new Enemy();
Example inside a function:

void CreateEnemy()
{
    {
        std::shared_ptr<Enemy> enemy = std::make_shared<Enemy>();
        enemy->SetMaxHP();
    }
    // Here, 'enemy' is deleted because it’s out of scope
}
To keep it alive, store the pointer somewhere persistent, like in a class:

class GameScene
{
public:
    std::shared_ptr<Enemy> enemy;
};

int main()
{
    GameScene scene1;
    bool gameIsRunning = true;

    while (gameIsRunning)
    {
        scene1.Update(); // enemy lives here
    }

    return 0;
}