Never free memory yourself! Do this instead (in modern C++)

preview_player
Показать описание
Arguably this is the most important video to understand how memory is allocated in C++. Even more, we cover not just how it can be allocated manually but how it allows C++ to be so fast as well as how to do it safely in modern C++.

*We cover*
⏺ What memory management is
⏺ Why garbage collection is not the way to go
⏺ What is stack and how it enables quick allocations and de-allocations of small variables
⏺ Why stack doesn't work well for persistent data
⏺ What is a heap and how it is useful to allocate persistent data
⏺ What issues can we expect with manual memory allocation
⏺ How smart pointers allow allocating memory safely

🙏 *Support what I do*
If you believe that free education is a good thing, you would like to see more of it, and you like my way of teaching C++, please consider supporting my efforts. I choose to not hide this course behind a paywall to keep it accessible for others. With your support you show me that teaching for free *is* a viable option. Here are the best options to support me:

🔗 *Links*
⏺ Donald Knuth picture is from Wikipedia

*Contents*
00:00 - Start
00:25 - What is memory management
02:10 - Why C++ is so cool
04:02 - Allocating and freeing memory
05:07 - The stack
06:18 - Stack hands-on example
08:06 - Why not allocate persistent data on the stack
09:42 - The heap
12:37 - Heap hands-on example
13:43 - Issues with manual memory allocation
18:45 - Smart Pointers

Corrections:
8:24 This example has many issues with it: not only this is not _really_ C++ as the dynamically sized C-style arrays are a compiler extension, not part of the language, but it also misses an include for std::byte. This is just to illustrate how we can overwhelm the stack.

#cplusplus #programming #coding #tutorial
Рекомендации по теме
Комментарии
Автор

About the manual delete, I always get questions from friends about whether they should use delete on a raw pointer they got as a return value from a function inside some old C library, It is all super confusing for beginners. Usually Documentation have it mentioned as "non-owning". It's amazing to see how all over the place each library's cleanup methods are.

segfault
Автор

How can I not like this video. Thank you Igor.~

kompila
Автор

This is quality content, your channel is underrated bro

advik-b
Автор

I’ve been watching your channel and following along from the start. This video just helped me tie a lot of concepts together. Many thanks, keep it coming!

Sheeldzy
Автор

Great explanation. Liked and subbed buddy

alifimtiaj
Автор

Amazing content Igor, keep up the good work.

pragyandahal
Автор

you always deliver a masterpiece related to C++. Thank you so much for sharing your knowledge :)

SanjeevKumar-nmps
Автор

Very nice video, thank you! What do you use for the code and diagram animations at the end?

Pedro-jjgp
Автор

Great video, however I couldn't figure it out how you change your T-Shirt that fast 😄.

glaubercini
Автор

It's great that people got interested by memory management, but I fully disagree with the usage of smart pointers and garbage collectors as a reliable and performant way of managing memory.
Those techniques assumes that every "object" has his own lifetime, witch is fundamentally false: When there's one, there's many. This affects performance because of memory fragmentation and loses capabilities.
I highly recommend the talk of Ryan Fleury about Arena Allocators, it's mindblowing how simple and effective a linear allocator can be. I almost never think about memory management and have all the performance and reliability.

JoseRomagueraM
Автор

Nice, and why did the presenter change shirts at 5:10???

richardbennett
Автор

Nice explanation! By the way, what font-style, and colour-theme are you using? It looks pleasant to the eye!

tirthasg
Автор

Great. Do you have any udemy course in detail for c++. I would like to purchase it

jojify
Автор

Then, he changed it back again at 6:05.

😂😮😅😅😅

richardbennett
Автор

why do a video? why not simple write a nicely written article that people can read at their own leisure, and one where you can correct the numerous issues raised on the cpp subedit

fjhfbuf
Автор

Nah man, RAII is there for a reason. There is no free lunch. It's an indirection that in many cases isn't necessary. Writing a quick free with a null check into a destructor is not hard. Smart pointers are great for some things, but a shared pointer is inefficient and a unique pointer is kind of obsolete in many cases. I think nuance would make this a bit better. They should not be your default move. You should always evaluate your options.

politejellydragon