Day 5/100 100 days of Code

Photo by RetroSupply on Unsplash

Day 5/100 100 days of Code

Delivering Safe C++ - Bjarne Stroustrup - CppCon 2023

Today I watched a fascinating talk about safe C++ where the creator of the language Bjarne Stroustrup talks about delivering a safe C++ application.

He suggests some best practices on safety and talks about the concept of profiles which can be used to set If there is a need for safety checks in the code that is typed or not. It would also be possible to mix them and remove safety checks if performance is needed in some other part of the code base.

Watch the talk here:

I also made some changes in the game I made in Global Game Jam. I removed some unneeded code and replaced the C-style for loops with range-based wherever it was possible. I did this in a destructor as the loops are used to free up memory that is being held by vectors.

emplate<>
Player<SDL_Surface, SDL_Texture, SDL_Renderer >::~Player()
{
    for (auto &i : frontAnimIdleFrameSurface)
    {
        if (i == nullptr)
        {
            break;
        }

        SDL_FreeSurface(i);
    }

    for (auto &j : frontAnimWalkFrameSurface)
    {
        if (j == nullptr)
        {
            break;
        }

        SDL_FreeSurface(j);
    }

    for (auto &k : backAnimWalkFrameSurface)
    {
        if (k == nullptr)
        {
            break;
        }

        SDL_FreeSurface(k);
    }

    for (auto &l : sideAnimIdleFrameSurface)
    {
        if (l == nullptr)
        {
            break;
        }

        SDL_FreeSurface(l);
    }

    for (auto &m : sideAnimWalkFrameSurface)
    {
        if (m == nullptr)
        {
            break;
        }

        SDL_FreeSurface(m);
    }

    if (frameTexture != nullptr)
    {
        SDL_DestroyTexture(frameTexture);
    }

    if (backAnimIdleFrameSurface != nullptr)
    {
        SDL_FreeSurface(backAnimIdleFrameSurface);
    }
}