Smart Pointers in C++ (unique_ptr vs shared_ptr vs weak_ptr)
JAGC Studio
0:00 / 0:00
Smart Pointers in C++ (unique_ptr vs shared_ptr vs weak_ptr)
12 просмотров · 12 дн. назад
JAGC Studio
36 подписчиков
12 просмотров · 12 дн. назад
What are smart pointers in C++? A smart pointer is a small object that holds a pointer to a heap object. unique_ptr and shared_ptr own the object and delete it when no owner is left. weak_ptr only watches it.
In this video: why memory leaks are dangerous in games, what a pointer is, a dangling pointer, how to write unique_ptr, shared_ptr and weak_ptr, and where to use each one. Then a small game project that uses all three, and real lines from the Unreal Engine 5.8 source code. We ran every example on our PC. A game loop that leaks 1 KB per frame lost 244 MB in about 69 minutes at 60 fps, and stopped at our 256 MB memory limit.
Subscribe for more game development, explained visually.
Chapters:
0:00 Why memory leaks crash games
0:24 What is a pointer in C++
0:38 Dangling pointer
0:54 What are smart pointers
1:08 unique_ptr and make_unique
1:24 shared_ptr and make_shared
1:45 weak_ptr and lock
2:06 Smart pointers in a game project
2:38 Unreal Engine smart pointers
3:00 unique_ptr vs shared_ptr vs weak_ptr: when to use each
Sources:
unique_ptr: https://en.cppreference.com/w/cpp/mem...
shared_ptr: https://en.cppreference.com/w/cpp/mem...
weak_ptr: https://en.cppreference.com/w/cpp/mem...
C++ Core Guidelines (R.20 to R.24): https://isocpp.github.io/CppCoreGuide...
Unreal Engine smart pointers: https://dev.epicgames.com/documentati...
Tests: our own C++ programs (MSVC, x64), run on our PC.
Which smart pointer do you use most? Tell me in the comments.
#cpp #gamedev #programming