Safety in modern C++ and how to teach it

Patreon ►
Twitter ►
Instagram ►
Discord ►

Series Playlist ►

Thank you to the following Patreon supporters:
– Dominic Pace
– Kevin Gregory Agwaze
– Sébastien Bervoets
– Tobias Humig
– Peter Siegmund
– Kerem Demirer

Gear I use:
BEST laptop for programming! ►
My FAVOURITE keyboard for programming! ►
FAVOURITE monitors for programming! ►

MAIN Camera ►
MAIN Lens ►
Second Camera ►
Microphone ►

20 thoughts on “Safety in modern C++ and how to teach it

  1. I agree with you 100% Cherno. You give a full understanding of how c++ works. Its not your responsibility to insure other peoples logic is sounds. You are giving a strong base of information to make the coding decision that are ultimately up to the programer.

  2. I once intentionally made a memory leak.

    It ended up not just crashing my program but getting my OS maxing out with memory causing everything else to crash with the memory not freeing up. I had to reboot of course.

  3. Smart pointers are like the spiderman suit made by Tony Stark. If you don't know how to write code without them then you should not use them.

  4. You can't control how others perceive you or what they do with what you teach. It's their responsibility to do their due diligence and make sure they are writing good code. I agree that teaching the "dangerous" fundamentals is good, people should know how things work from the ground up. I'm not sure how you would even be able to understand memory safety if you didn't know how raw pointers worked.

  5. To be honest I sometimes use smart-pointers, but when I'm not, I'm in some way rewriting the thing because I'm much more comfortable with my code. ONLY using "Old Pointers" is like saying we should still be using assembly, because we have pretty much 99% control. But should we? how hard is printing Hello World in Assembly, and how hard is it in C++, or any other language?
    So personally I think it depends.

  6. I am all for smart pointers, unless they are killing my performance or a smart pointer won't work in that situation. I am like that for anything that aids me and my team to our job.

  7. I realize that the cost of reference counting is incredibly tiny on modern hardware, but I still find the concept just ever so slightly offensive, If you're going to reference count everything, why not garbage collect as well? I mean by the time you implement all the thread guarding you're going to need, you just about might as well? And if you're going to do that, why not just use Java or C# or Python.

    I feel like corporate C++ programmers, more often than not, are doing something that is tantamount to escorting an elephant a half mile home with a fighter jet. This one group I encountered recently wrote this giant C++ system that does business logic processing of database data. So they're using C++ to make sure they save every microsecond they possibly can while waiting hundreds of milliseconds for database data. And of course they set this up as network-accessed microservice, so they have serialization and data marshalling through a whole transport stack going on. So they're saving microseconds while waiting milliseconds for that process, too. And they wrote a custom caching system and a custom ORM for their giant C++ system. So they've, like, cleared a 20 mile radius of airspace around their elephant while they escort it a half mile home with their fighter jet. It would be laughable if I weren't faced with maintaining it. You guys can laugh. I'm. Not laughing.

    If you're escorting an elephant, and you're perfectly fine with reference counting everything, than jeepers, screw C++! Use Python or something. Use C# and linq to entities. You don't need to write a custom ORM, for crying out loud. And operating systems and relational database management systems are really amazing at caching large volumes of corporate data for you. Use pl/sql to perform your aggregate calculations of database data. Save your C++ energy for when you actually need it.

    If you actually need C++ for what you're doing.. I'm not yet convinced that reference counting is something you really want to be doing very much of? I might be wrong, but. I mean by the time you reference count everything and do all the thread guarding you'll need if you do that in a generalized way.. it's pretty darned far from being a zero overhead abstraction? It is possible to make C++ go slower than Python. In fact it's not even too terribly hard.

  8. I could see only a couple reasons for using "raw" pointers. 1. You're using Qt's memory management system. 2. You've implemented your own parent-child object memory management system similar to Qt's. When you get down to it, really, only point 1. makes sense. But maybe point 2. could work if you're writing your own libraries.

  9. One main advantage of using smart pointers is exception safety. If an exception is thrown before you call delete, then it will not get freed and thus leak. Smart pointers guaranteed to clean up memory always.

Leave a Reply

Your email address will not be published. Required fields are marked *