Modern C++: Upgrade Your Skills with Shared Pointers!

  Рет қаралды 62,708

Dave's Garage

Dave's Garage

Күн бұрын

Пікірлер: 184
@billyofthefuture
@billyofthefuture Жыл бұрын
Note that std::atomic_load and std::atomic_store on std::shared_ptr arguments are deprecated as of C++20. If you need to atomically change a std::shared_ptr, you should be using a std::atomic nowadays.
@wafikiri_
@wafikiri_ Жыл бұрын
I'm curious about the reason of such a deprecation. Could you please tell me what kind of issues provoked it?
@isodoubIet
@isodoubIet Жыл бұрын
@@wafikiri_ Because the above syntax was essentially a hack to simulate the semantics of a real atomic, which didn't exist in C++17 but does exist in C++20. Now that the correct type exists you should use that instead. I recommend the 2-part talk "Juggling razor blades" by Herb Sutter which talks (among other things) precisely about the motivation for atomicity with shared_ptrs.
@wafikiri_
@wafikiri_ Жыл бұрын
@@isodoubIet I much appreciate your soon and clear reply. I learned C++ in the 1980's, but never had the chance to program in C++ (then, I used to program in Assembler for the Motorola 65xx series). But I still remember clearly the concepts that fund C++, not so much the specific syntax, but I can still read a C++ program and more or less understand what it would do. New or refined concepts like unique, shared, weak, and atomic pointers are a great contribution. But they may push my limits somewhat. Although currently retired, I'm hobbyishly designing a processor with a decoder that allows loops and conditional or inconditional branching in microcode machine-code decoding routines. I just designed a 256 one-byte machine code table (wish one byte could allow for more, but maths is what it is) with multiple addressing modes for two accumulators or memory, and many other features such as support for multi-tasking and selected-areas conjoint memory paging, and every new concept I find out there helps me design better.
@toby9999
@toby9999 Жыл бұрын
That gives me an extra 10 years... we still haven't adopted C++11 yet. Lol Something like 90% of our codebase was developed in the 90s.
@Inuyasha463
@Inuyasha463 Жыл бұрын
Very cool video. I hope you keep doing these. Last time I used C++ was back in college before all these were added to the language, so it's nice seeing how things have changed.
@Yakeru35
@Yakeru35 Жыл бұрын
Thank you ! I have very little experience in C++, but I am learning Rust currently, after many years of Java, just out of curiosity, and to satisfy a desire to get closer to the hardware. This video helps me to understand many of the reasons behind Rust's choices. Rust looks like C++ with guard-rails, but the guard-rails are here if you know them, and use them correctly, which makes it look less "scary" than it really is. Makes me want to study C++ again :)
@SwapChain
@SwapChain Жыл бұрын
Thanks for the super explanation. I hope this playlist gets more videos. It is very exciting to learn from someone who has been involved with the technology for so many years.
@johnmckown1267
@johnmckown1267 Жыл бұрын
Excellent. I haven't looked at C/C++ for a lot of years. The new "smart" functions are brilliant.
@nextlifeonearth
@nextlifeonearth Жыл бұрын
Not that new, they're over a decade old. Still relevant though.
@The_Gray_Zone_Man
@The_Gray_Zone_Man Жыл бұрын
Thank you so much for the effort , I've learned something today. These in-depth tech themes are very vital and crucial to future programmers and I'm waiting for more and more to come .Thank you again , always looking forward !
@pawnslinger1
@pawnslinger1 Жыл бұрын
I used to code in C/C++ when I was working - but after I retired, I guess I looked for something different... I started with a MITS computer and the Extended Basic on cassette tape. So I went looking for a good modern Basic.... and I found Visual Basic 2008 and nowadays 2017.... it is at once Basic, but with a heavy dash of Class structure and a vastly improved function library. In my retirement (some 20 years now) I have completed several major projects (largely for myself) with Visual Basic, and I love it!!! I even did a minor project to create a clock with multiple threads using the Basic interrupt routine. Very neat.... I wanted a digital clock with giant numbers so I could see them (I am sight challenged due to diabetes)... and after awhile, I hung several things off that program, like some code to play mp3s in the background while I coded. But I guess there's no chance of you covering VB, and most of the sample code I find online to help VB users is in C#, so I am having to learn that in self-defense. So C# coverage would be a great help for us VB users.
29 күн бұрын
damn such a long history of code. Why does it make me feel nostalgic to read your past experiences lmao.
@scriptles
@scriptles Жыл бұрын
Learning to program videos are truly the best. I love watching the Cherno's channel and if you started doing these I would love to watch yours too.
@markmuir7338
@markmuir7338 Жыл бұрын
Nice introduction to this topic. One suggestion: simple but realistic/practical examples are most helpful to newcomers. The fclose() custom deleter was a good one. With weak_ptr I would recommend showing something like a Document containing a Page containing a Paragraph. Start with parents owning their children as a shared_ptr. Then add back-pointers. Initially used shared_ptr for these too, but then demonstrate how cutting a Page from one Document and pasting it to another would create a memory leak. Then show how weak_ptr would fix this.
@keithlambell1970
@keithlambell1970 Жыл бұрын
Thank you for an excellent description. I am less confused now about the copying of shared pointers.
@jackpisso1761
@jackpisso1761 Жыл бұрын
Thank you so much for making these. I was stuck with C++ 98.
@SamBebbington
@SamBebbington Жыл бұрын
I learned C++ at uni, but we were taught 98. Shocking given that I left uni less than 5 years ago! These videos are brilliant and I'm so glad I can actually understand new things written in C++ (and write it too!).
@absalomdraconis
@absalomdraconis Жыл бұрын
Many things you don't need from the new versions, but smart pointers and threads you should learn.
@nextlifeonearth
@nextlifeonearth Жыл бұрын
​@@absalomdraconis Need? Probably not necessarily, no. All new features are worth looking into though and can be incredibly useful. Especially for optimisation there are a lot of new features that are pretty amazing. The features like constexpr and span/string_view are surprising they weren't in earlier C++ versions (and in the case of constexpr I'd say C could use it too). But moreso than smart pointers, you should learn RAII in general so you could even build your own (you won't have to, but you will at one point need to do something like that).
@anon_y_mousse
@anon_y_mousse Жыл бұрын
@@nextlifeonearth For the most part I agree, but I dislike constexpr so much. Not that I disagree with the functionality, but the way it is accessed. Instead of introducing new keywords I would've preferred changing how some others are used, but as we all know, they're not going to do that because it might break compatibility with older software. So now we've got a rat's nest of keywords and symbols. R-Value references anyone? My take is that ultimately a group of us should get together, design a better language with all of the good features of C++ and move to that instead. If anyone recommends Rust they're out of the group, because it's worse than C++.
@lucasmendesorg
@lucasmendesorg Жыл бұрын
@anon_y_mousse That would break compatibility with old projects that expects keywords/functionalities to behaves the way it was initially designed. That regarding syntax and to avoid ambiguity. That’s the reason C++ castings is different from C (and explicit), {} initialization, and even lambda syntaxes are the way they are.
@anon_y_mousse
@anon_y_mousse Жыл бұрын
@@lucasmendesorg Yeah, and that's why I said it'd be easier to just come up with a new language because no one wants to break compatibility even though it would be better.
@Nik930714
@Nik930714 Жыл бұрын
I've not used C++ seriously in absolute ages. I never knew they've added so many cool stuff in that language. I rarely write code nowadays, but when I do its usually C or C#. I've been on the receiving end of pointe problems for a long time, so seeing them being mitigated somewhat in an old language looks great. Still new code in the company I work for that used to require C will probably be done in Rust or Zig. We are still evaluating them for embedded applications.
@puppergump4117
@puppergump4117 Жыл бұрын
Apparently c++20 has coroutines for threads which people say are pretty nifty. They also have modules, which while buggy, are able to vastly increase compilation time by eliminating the need to differentiate between cpp and hpp files. I've also heard many c++ users say that Rust isn't all it's cracked up to be, but maybe that's just them being too attached lol.
@anon_y_mousse
@anon_y_mousse Жыл бұрын
@@puppergump4117 No, it's just because Rust is a worse version of C++. They took some of its mistakes and made them worse, and then made all new ones of their own.
@puppergump4117
@puppergump4117 Жыл бұрын
@@anon_y_mousse Hey that's how my parents describe me
@anon_y_mousse
@anon_y_mousse Жыл бұрын
@@puppergump4117 Yeah, probably all parents when they learn their children have used Rust. It's a sad epidemic, but you can get back on their good side by using only C around them.
@VultureGamerPL
@VultureGamerPL Жыл бұрын
Your channel is truly a goldmine of knowledge. Thank you for the time and effort you put into these videos.
@amaama4140
@amaama4140 Жыл бұрын
Thank you for this quality learning material. We are all eager for similar content on the C++ language.
@rickpoeling6831
@rickpoeling6831 Жыл бұрын
Another great instructional video! I look forward to you making more of these C++ videos. One on proper techniques for handling multi-threaded programming would be great.
@MitjaSkuver
@MitjaSkuver Жыл бұрын
Pretty sure it would go: I am a Base. I am a Derived. I am a Derived. Great stuff, like the more advanced topics. And I don't even do C++ :)
@nextlifeonearth
@nextlifeonearth Жыл бұрын
Gotta say this single video used more shared_ptrs than in all my codebases combined. I generally prefer a unique_ptr at all times and tend to redesign before I use a shared_ptr.
@DavesGarage
@DavesGarage Жыл бұрын
Maybe you missed my unique_ptr video, but this video was intended to show shared_ptr, so somewhat unavoidable
@tomysshadow
@tomysshadow Жыл бұрын
Incidentally I was just in the middle of rewriting one of my old programs to use these instead of raw pointers. The unfortunate thing is that I knew the name unique_ptr when I was originally creating it but never looked into what it did because I assumed it was overcomplicated for what I needed to do. In reality, using it would've been simpler and saved me time...
@scienceandmathHandle
@scienceandmathHandle Жыл бұрын
Dave I would love it if you did a video on lambdas and the std::algorithm library! Thanks for the videos, these are great, I am sending these to tons of people.
@dougpark1025
@dougpark1025 Жыл бұрын
Smart pointers are a great feature of C++. One that has been employed long before they showed up in the boost libraries. With C++ 11 the addition of official support of smart pointers in the STL has been a great improvement. You touched briefly on circular references to smart pointers. I suspect that many who are watching this didn't comprehend the importance of this problem. When I use shared pointers I try to follow the rule that there is a clear master owner of a shared pointer. One that remains in control for the lifetimes of all others who share the pointer. I rarely have found the need for a weak pointer, but it certainly has a great deal of value in certain cases. One problem that you can run into with shared pointers is performance. As you pointed out a weak pointer may help, but there are times when it is very reasonable to drop down to a raw pointer to gain performance. Although one must be careful not to prematurely optimize.
@absalomdraconis
@absalomdraconis Жыл бұрын
For single-threaded code (a multi-threaded version forever remained an experiment for the future) I've actually played with smart pointers that are loop safe (most recently in C instead of C++, my approach has splay-tree-like speed characteristics)- it seems to certainly be possible to have multi-master loop-safe smart pointers, but in the absence of Rust-like additional restrictions on behavior, I think it's innately somewhat slow & somewhat heavy to actually do.
@nextlifeonearth
@nextlifeonearth Жыл бұрын
If you have one owner, then a unique_ptr makes more sense anyways. I generally prefer a redesign over the use of a shared_ptr. They're evil. Not really, but since I got a deadlock with a smart pointer I'm going to try avoiding those at significant expense.
@arnoldfossman1701
@arnoldfossman1701 Жыл бұрын
Hey Dave! Being an old codger who is just starting to learn a bit about writing code this stuff is over my head a tiny bit. However, this video is getting a like from me because as I learn to code and I reach a level where I can use this info I will know where to look for it.
@absalomdraconis
@absalomdraconis Жыл бұрын
In essence, smart pointers do boring boilerplate for you. When using them you want to know their quirks so you can compensate, but all of the time you want to know how to build them, so you can auto-manage _your own_ stuff.
@gtdcoder
@gtdcoder Жыл бұрын
Wow its amazing so many people are just learning this stuff now. Most of this has been standard best practice for decades.
@SassyToll
@SassyToll Жыл бұрын
Thanks again Dave, these videos are really helping me with C++, I am learning so much
@muddyexport5639
@muddyexport5639 Жыл бұрын
Thank you! This was great info. Really appreciate you taking the time to explain. Typically not done; assumed by the narrator. You performed the function as a teacher. So, thanks professor!
@andychess
@andychess 11 ай бұрын
Great video Dave:-). Just what I needed to clarify what, for me, is a quite dry and abstract subject. I'm writing C++ on the ESP32 and would love to see some content featuring both if there is an appetite for it. Keep it coming please, I really enjoy your work.
@manuel_youtube_ttt
@manuel_youtube_ttt 10 ай бұрын
Love these C++ videos, they are so clear!
@kissinger2867
@kissinger2867 Жыл бұрын
Another amazing video, concise and clear explanation, thank you Dave.
@OlliS71
@OlliS71 Жыл бұрын
The overhead of maintaining the reference-counter isn't tiny because the rerence-counter is updated atomically. If you have a L1 cache hit on that that's at best 20 clock cycles. Copying a pointer from register to register can be done about 80 times faster on my CPU.
@DavesGarage
@DavesGarage Жыл бұрын
My code runs fast, that's all that matters. Optimize the important paths, keep the rest safe
@OlliS71
@OlliS71 Жыл бұрын
@@DavesGarage shared_ptr is thought to share objects between threads, nothing more. Therefore the performance-penalty is o.k.. Depending on how often you copy a shared_ptr within a thread, this can become a problem.
@不知火知了
@不知火知了 5 ай бұрын
Very good video that deepened my understanding of smart pointers
@Rcomian
@Rcomian Жыл бұрын
this is a great series, thanks for putting this in one place
@pryflack3646
@pryflack3646 Жыл бұрын
Here’s one for your dopamine 👍 ;) Coming from high level languages, and having already dipped my toes in Rust a bit, it’s interesting to see that C++ has solutions to address memory intricacies. A detailed comparaison of features between the 2 languages would help in discerning fact from exaggerations, and make a great watch.
@absalomdraconis
@absalomdraconis Жыл бұрын
Unfortunately, C++ spent a while semi-stagnant. It's standard library was smaller than it should have been for quite a while (as opposed to Java, which is basically the opposite), which in combination with it's trap-ladden syntax (so, what does T< A< 3 >> do? For quite some time, _not what you think)_ greatly hindered it's use. This is why so many C++ killers (C#, maybe Swift, certainly Rust and Zig, somewhat Go) popped up, and C regained market share- there's a very real market for a C++ killer, and while recent progress has relaxed that, it's still very much the case.
@Christian-op1ss
@Christian-op1ss Жыл бұрын
Excellent video Dave, thank you very much. I hope you continue this series!
@PaulPluzhnikov
@PaulPluzhnikov Жыл бұрын
At 7:56 -- "first sp1, sp2 and sp3 will be destoyed ... then the vector" -- is incorrect: the vector will be destroyed first, then sp3, sp2 and sp1 (in reverse order of construction).
@richardclarke376
@richardclarke376 Жыл бұрын
Great stuff and glad to see you back after your surgery !
@naukowiec
@naukowiec Жыл бұрын
Thanks for sharing a concise explanation of pointers, I'm sure it will help many get into C++ without the pointer anxiety ^_^
@thecodesorcerer
@thecodesorcerer Жыл бұрын
Good stuff, great concise explanation. Also enjoy the 6502 stuff.
@FrancisBurns
@FrancisBurns Жыл бұрын
Hi Dave! Love your content! If you can recommend c/c++ projects for beginners ready to take it to the next level, we would be very grateful!!
@metasquierraptor4125
@metasquierraptor4125 Жыл бұрын
Great video! Would be great to see the code being run so we can see the outputs as your explaining it.
@chrstfer2452
@chrstfer2452 Жыл бұрын
Oh that weak pointer shared pointer thing is neat.
@petevenuti7355
@petevenuti7355 Жыл бұрын
The jump cut at 9:48 , how long did it take to line up that little detail of the slight angle you were sitting back at? 9:48
@Neeseius
@Neeseius Жыл бұрын
I like c++ and found this video very satisfactory. A 👍 was also provided.
@muratsahin1975
@muratsahin1975 Жыл бұрын
Constant flow of pure information. Great.
@chewy465
@chewy465 Жыл бұрын
Isn't sp3 redefined as derived as well at the time you add it to vector?
@otmanm4095
@otmanm4095 Жыл бұрын
That serie of c++ vid' is awesome, thx!
@johnburgess2084
@johnburgess2084 Жыл бұрын
Thanks for this technical content. Consider your dopamine tickled!
@theintjengineer
@theintjengineer Жыл бұрын
Thanks for the C++ content 👌🏽
@uberDoward
@uberDoward Жыл бұрын
Dave, qq - where is sp2_alias defined? I seem to be missing that - is it a class constructor, or a method, or what?
@LanceMcCarthy
@LanceMcCarthy Жыл бұрын
The joins were interesting. This is why I love async/await Task in C#, I can easily manage other threads and whether or not they return to the same context or not.
@mcmaddie
@mcmaddie Жыл бұрын
It's been such a pain that they've actually added new thread type that automatically joins on destruction called jthread. That came on C++20 standard.
@phildem414
@phildem414 11 ай бұрын
Great series! Hungry for more❤
@di380
@di380 47 минут бұрын
Thanks for the video and the explanation. I have one question regarding the use of smartpointers with C++. I have only used template classes with standard C so I try to avoid using C++ as much as possible because of the confusion but shouldn't a smart pointer call the appropriate destructor of the class whenever the reference count goes to 0 and the object is destroyed then why would to use a custom deleter? Unless this is not the case and the destructor only gets called if you explicitly perform a delete on the object. This has me a bit confused
@jsergiuiulian
@jsergiuiulian Жыл бұрын
Hi Dave. Great explainer, thanks for this (and for all the other content)!
@DavidRomigJr
@DavidRomigJr Жыл бұрын
“I am a base for sp3” (in regards to the vector of shared_ptrs. Wait, wouldn’t it be “I am a derived” because sp3 was reset to Derived? I’ll have to run this later to double check. I like learning new things.
@mvlad7402
@mvlad7402 Жыл бұрын
Subscribed, great info on C++ intricacies!
@bean_mhm
@bean_mhm Жыл бұрын
Loving the series!!
@wafikiri_
@wafikiri_ Жыл бұрын
While some of your explanations are pretty clear for me, some other aren't. I got lost with the atomic stuff (and someone commented you're using atomic functions now deprecated in C++20, so, they must have an issue). For visually-oriented people like me, even a simply drawn sketch on paper (of what is being managed and how, what issues could arise, and what the strategy is to elude them) would be an invaluable complement that could better convey understanding than just oral explanations.
@DavidMcCurley
@DavidMcCurley Жыл бұрын
Learned something new. Thanks!
@abacaabaca8131
@abacaabaca8131 7 ай бұрын
Although I am very confuse when you say "object" all the time, when you're referring to shared pointer instance. Since it is a class, and an instance of it would be an object right? When you say object do you mean the pointer itself on the stack or do you mean the data on the heap?
@jorge28624
@jorge28624 2 ай бұрын
there is people building stuff like game engines and avoiding STD at all, I believe all this facilities for better handling of pointers, for performance and binary size sake. Is that a good idea?
@guilherme5094
@guilherme5094 Жыл бұрын
I have come here to chew bubble gum and learn C++. Thanks Dave👍!
@АнатолийБугаков-е9г
@АнатолийБугаков-е9г 11 ай бұрын
in the main, even without container/vector, I get "Derived destroyed" twice, "Base destroyed" three times, and "Custom dleeter called" once. Why? We only have 3 objects: behind sp1, sp2 and sp3. It doesn't add up. UPDATE: this is because when Derived is destroyed , Base destructor is also called. Why is that?
@genelee7869
@genelee7869 Жыл бұрын
I haven't programmed in C++ in decades (I moved to C# in early 2000s and haven't looked back), but it seems all these hoops to jump through for shared pointers are what C# does all automatically under the hood. What am I missing?
@nicoladellino8124
@nicoladellino8124 Жыл бұрын
Very useful video, THX Dave.
@rikardottosson1272
@rikardottosson1272 Жыл бұрын
Why will the for loop over shared pointer show sp3 is a base when you replaced it with a new Derived()?
@mandolinic
@mandolinic Жыл бұрын
I'm getting some deja vu. Yup, Java had all of this in the 1990s with far cleaner syntax. Nice to see C++ being dragged into the 20th century ;-)
@JBrinx18
@JBrinx18 Жыл бұрын
Ooh, very interested in multi threading. Trying to figure out the best way to do it since I have a couple of changing structs and constant member variables
@absalomdraconis
@absalomdraconis Жыл бұрын
In general: 1) keep as much stuff in a single thread as possible, 2) when you can't, keep it in a single thread before then _transfering_ it to a second thread, 3) when you still can't, carefully coordinate between threads. In general, the best way to multi-thread is to coordinate between threads as little as possible. Having a bunch of disconnected threads that only cross-communicate when handing in completed work, or getting the next batch of work, is generally the best choice available.
@__hannibaal__
@__hannibaal__ Жыл бұрын
I stop programming in 2003; and engage in complex analysis and differential manifold, and many of mathematics field like Hamiltonian mechanic , continuum medium ; and now I m very wonder how C++ became powerful ; and now i return back after 20 years;
@NonTwinBrothers
@NonTwinBrothers Жыл бұрын
pointers are so cool. They should invent an auto pointer
@leduyquang753
@leduyquang753 Жыл бұрын
`std::auto_ptr` came before `std::unique_ptr` and `std::shared_ptr` and was removed from C++ in 2017.
@NonTwinBrothers
@NonTwinBrothers Жыл бұрын
@@leduyquang753 I auto-matically realized the weakness of my joke
@jonathanwilson8809
@jonathanwilson8809 10 ай бұрын
So is this like an Rc in rust?
@kwazar6725
@kwazar6725 Жыл бұрын
What about cyclic references?
@danman32
@danman32 Жыл бұрын
I'm still dizzy about classes, particularly inherited and derived classes That's why I don't get how the Adafruit GFX library can be used with all sorts of display hardware libraries. And why I couldn't understand why the graphics privatives such as what's included in Adafruit can't be used with WS2812b matrices. I failed miserably trying to follow along with that clock demonstration
@danman32
@danman32 Жыл бұрын
And what all those symbols mean. Like "
@MrRmeadows
@MrRmeadows Жыл бұрын
Why the custom delete; How is it better than implementing ~?
@axllbk
@axllbk Жыл бұрын
I'm interested in more!
@IIARROWS
@IIARROWS Жыл бұрын
Wouldn't just be better to use a destructor instead of a custom deleter in this example?
@billyofthefuture
@billyofthefuture Жыл бұрын
no because FILE* comes from C stdio and has no destructor. You'd have to write a whole class to wrap it. shared_ptr is much less work than that.
@IIARROWS
@IIARROWS Жыл бұрын
​@@billyofthefuture But we have a class... the class Derived.
@billyofthefuture
@billyofthefuture Жыл бұрын
@@IIARROWS that was a different earlier example. The custom deleter example was at 16:30 or so, wrapping a FILE* and he says we are not going to have to write a class. Which is great.
@IIARROWS
@IIARROWS Жыл бұрын
@@billyofthefuture Yes, I'm talking about the first example... in which there is a custom deleter. 4:41
@tomysshadow
@tomysshadow Жыл бұрын
It's true that you could write a whole class to wrap the C style FILE pointer into a C++ object, and if you're going to be reusing it a lot that's probably the smart way to go. Still, custom deleters or scope guards allow you to one-off quickly adapt a C style interface with open and close functions so that the close function is called even in the event of an exception. This is especially useful with WinAPI, where despite attempts to adapt it into a C++ way of thinking (such as MFC,) you basically have to learn a whole new API because they barely resemble the original anymore - in contrast to just using a scope guard, shuffle a couple lines around and you're done
@jackgerberuae
@jackgerberuae Жыл бұрын
If buffer overflows and memory leaks etc can cause security exploits, how difficult would it be for the nefarious characters to manipulate the pointer counters sitting there openly counting pointer movements?
@MrSephirothJenova
@MrSephirothJenova Жыл бұрын
Not particularly difficult to corrupt reference counters if you find some way to access the heap, but by then the program is already completely compromised anyway
@isodoubIet
@isodoubIet Жыл бұрын
Yeah, probably the most common vector of attack in such situations would be to exploit an existing bug in the code by figuring out when it happens (e.g. a reference cycle that causes a memory leak) and then making it happen reproducibly.
@BryanAnderson-o1u
@BryanAnderson-o1u Жыл бұрын
interesting i did not know any of this stuff existed by the way Dave i want to program in assembly on windows 11 but i dont know how or were to start i have been programming in c++ for a while but assembly is the real stuff i at least want to make a few simple little games in assembly because i think ill be very satisfying so basically could you make a video on how to code assembly on windows 11
@tomjohn902
@tomjohn902 10 ай бұрын
Maybe next make video of std::move. How to avoid copying objects/vectors, episode about string and string_view, using references effectively instead of pointers.
@vilijanac
@vilijanac Жыл бұрын
Shared pointers, primitive first version, of garbage collection in a computer language. Mine language (Perl) I only use today, has this inbuilt. Perl can also be nasty with its inbuilt vectors, when splice.
@bhanubhattarai13
@bhanubhattarai13 Жыл бұрын
Is it similar to Rc in Rust?
@dynfoxx
@dynfoxx Жыл бұрын
Yes though RC is for single threading where shared pointer always uses atomic. Shared pointer is closer to Rust ARC.
@juanmacias5922
@juanmacias5922 5 ай бұрын
More C++ episodes.
@mattwilliams1844
@mattwilliams1844 Жыл бұрын
Hey Dave! As always solid video, I'd like to point out how important I think modern C++ videos are and why I think you should make more of them. Like the fellow below me, I work in embedded systems/ micro-controllers like the STM32, Cortex so on and so on. There seems to be a big push towards Rust these days, not a bad language or anything. But all the people (that I work with atleast) seem to have a very poor understanding of how to write modern C++. People seem to act like it is just so unsafe, like it has no lifetime management like Rust etc. Now I think I know why this is, its an older language with lots of legacy bits you're not supposed to use, but I still feel it is over looked and misunderstood. So more advanced videos on the language would be greatly appreciated, I think a I know fair bit about the language, but I think you would know more (especially with your background).
@dynfoxx
@dynfoxx Жыл бұрын
C++ does not have lifetime management only RAII. Rust has both lifetime management and "RAII". RAII allows for a method to be called at the end of a scope. Lifetimes track and understand a variables use between scopes and without the need for destructor. Both are useful but they are not the same thing. Lifetime management is more powerful.
@mattwilliams1844
@mattwilliams1844 Жыл бұрын
@@dynfoxx Ahh thank you for the clarification
@hanysaad000
@hanysaad000 Жыл бұрын
Hi Dave, I really like your content which coming from an expert and was wondering if you can develop some kind of "Modern C++ from zero to GOD" course or refer me to a good one from someone who knowa how the compiler is interpreting and compiling and generating code. Please I will be waiting for your reply, Thanks!
@DavesGarage
@DavesGarage Жыл бұрын
I don't honestly know! I learned over time, but I'd surf the KZbin results for C++ tutorial and see what comes up! Look for something with a ton of views, which probably means its watchable.
@Skcoica
@Skcoica Жыл бұрын
love your vids
@N....
@N.... Жыл бұрын
Smart pointers are not "advanced", they are the basic building blocks of maintainable code and should be taught early on
@Stumpy-mr9rv
@Stumpy-mr9rv Жыл бұрын
I think advanced is used here because C++ didn't add them until C++11 and vanilla C++ doesn't have them.
@absalomdraconis
@absalomdraconis Жыл бұрын
​@@Stumpy-mr9rv : I haven't actually touched C++11 or later (I generally gave up on the standards committee, and moved to C), but before C++11 the C++ library was in many ways _primitive._ The language should have gotten simple shared pointers and thread capabilities around 2005, not 2011. I'd have to say that a non-string "Rope" template should have been added with the rest of the common containers.
@N....
@N.... Жыл бұрын
@@Stumpy-mr9rv What do you mean by "vanilla" C++? It was always intended to be an evolving language, according to the creator himself. Also you forget the history of std::auto_ptr and the times before a standard library.
@MePeterNicholls
@MePeterNicholls Жыл бұрын
@@N....absal’. is a giggachad
@JakobKenda
@JakobKenda Жыл бұрын
​@@N.... I think they meant it's not part of the language but the standard library.
@Lion_McLionhead
@Lion_McLionhead Жыл бұрын
Lions are 1 of those animals who program C++ as C with classes. There are so many corner cases with reference counting & the code is so cluttered, the pointer templates normally end up being a wash.
@KeithKazamaFlick
@KeithKazamaFlick 9 ай бұрын
thanks
@marcelocerri3779
@marcelocerri3779 Жыл бұрын
Once the language standard has 2 thousand pages everything can be considered "advanced"
@markowens5446
@markowens5446 Жыл бұрын
Nice tips
@ShadowRadiance
@ShadowRadiance Жыл бұрын
Mmmm... consider your dopamine tickled!
@Master_Media33
@Master_Media33 Жыл бұрын
@FreedomIsNotGoingToBeFree
@FreedomIsNotGoingToBeFree Жыл бұрын
So C++ invented VB6 20 years late? :) How does it solve circular references which is solved by GC, but not ref counting (AFAK(?
@dynfoxx
@dynfoxx Жыл бұрын
Not all GC detect circular references. Weak pointers are used to break circular references when reference counting.
@ConsultingjoeOnline
@ConsultingjoeOnline Жыл бұрын
Lol. What happens when your shared pointer cracks down on shared passwords. Lol 😆 🤣
@v9turner
@v9turner Жыл бұрын
I don't share my pointers with anyone !
@pedalinpete
@pedalinpete 4 ай бұрын
Modern C++ is a bit of a disaster. Have a read of Meyers' book. It's just too complicated for most people to use effectively. If you want these modern features use a language that was designed from the start with them. Just my humble opinion ;)
@emmanuelbeaucage4461
@emmanuelbeaucage4461 Жыл бұрын
so many people assumed I was dumb because i'm asperger that i'm scared not to understand... 😢
@byronwatkins2565
@byronwatkins2565 Жыл бұрын
These instructions are VERY poorly documented. The descriptions use words that seem to have "computer science" definitions that differ from Webster's and these definitions are not disclosed. Nor are there sufficient examples demonstrating the similarities and differences so that we might deduce the definitions for ourselves. I have tried several times to use these instructions but, once I kluge code enough for it to compile, it does something entirely different than I need. So, I gave up and use what I know as safely as I can manage. In fact, the descriptions SHOULD address the need for each instruction FIRST, the strategy that addresses that need second, and several examples of usage third. If it is always possible to use these instructions, then why not remove the unsafe constructs entirely and introduce "C+=2"
@s1lkysl1m83
@s1lkysl1m83 6 ай бұрын
i hate modern c++
@Ilovenazism
@Ilovenazism 6 ай бұрын
I love it
@zemlidrakona2915
@zemlidrakona2915 Жыл бұрын
C++ shared pointers are a really bad implementation. Better to take a few minutes and write your own.
@pdany86
@pdany86 Жыл бұрын
What would you do differently and why? I would avoid shared ownership in the first place unless I really need it. If we need something with the same capabilities what would be better? Why is shared_ptr bad? What C++ is intentionally missing is a non-threadsafely reference counted shared pointer which would be slightly more efficient.
@zemlidrakona2915
@zemlidrakona2915 Жыл бұрын
@@pdany86 There are a few things wrong with it. There is no way to turn off thread safety if you aren't using it. There is no way to get rid of the the weak counter if you aren't using it. Also, unless you know what you are doing you can easy create two allocations for a shared object, but at least there's a solution to that. By far the biggest problem though, is it's 2X the size of a regular pointer. Since shared pointers are easy to implement, I see no reason to use one tailored to the most general usage, since you will typically rarely need all those features.
@isodoubIet
@isodoubIet Жыл бұрын
@@zemlidrakona2915 " There is no way to turn off thread safety if you aren't using it." On the other hand, I've never seen a valid use case for shared_ptr that didn't involve concurrency, so I don't see why you'd turn off the thread safety even if you could.
@DavesGarage
@DavesGarage Жыл бұрын
I'm not saying it's so in this case, but "Write your own" is usually said by people who don't understand the provided ones.
@zemlidrakona2915
@zemlidrakona2915 Жыл бұрын
@@DavesGarage If you think I'm wrong then make your argument. Do a sizeof yourself and compare it to the the size of a normal pointer. People used to do ref-counting in C++ long before it was added to the standard library, and the way they usually did it was put the count in same memory block as the object. The only very limited case where the separate control block is desirable, is where you have large objects and are also using weak pointers So yeah, feel free to use it for that. But in other cases it just drags down your code.
@lucidmoses
@lucidmoses Жыл бұрын
There sure is a lot of talk a fuss about C++ memory management. This is coming pretty close to a solution with out a problem. I mean really, Other then in school or just general new to C++ people, how often to you see memory leaks/corruption and stuff in the real world.
@threeMetreJim
@threeMetreJim Жыл бұрын
It's got to be bullet proof, so that chat GPT can code.
@lucidmoses
@lucidmoses Жыл бұрын
@@threeMetreJim I've seen ChatGPT code. Bullet proof isn't going to make it's code compile. :p
@MiketheBassMan
@MiketheBassMan Жыл бұрын
If you have a very large system with persistent threads and processes sharing memory and communicating, plus I/O delays and error handling, the surface area for memory management bugs is enormous. I work on a product which is among other things an LTE/5G gateway and the rare bugs that escape to the field are most often memory management issues. These types of bugs are never trivial to identify and tend only to appear at full operational scale, so it makes sense to use smart pointers to make them impossible.
@lucidmoses
@lucidmoses Жыл бұрын
@@MiketheBassMan My project was over a million lines of code 8 years ago when I stopped checking. It talks to 250 hand held computer, 1000 printers (and other devices) . All via IPv4 and IPv6. Each hand held will allocate 200-1000 memory items.The app stays running 7/24 for a month at a time (stupid accounting requirement means it needs to be restarted when the period changes). In the last 23 years I've had 2 memory problems and one of those turned out to be fake as it was just an order of operation problem during shutdown. The other was when a customer bought the first hyper-threaded computer and that blew up my atomic locking. So Yes, Memory management problems are rare even on large,complex apps. But I do agree that they are a pain to diagnose. Especially that fake one which only ever happened in production. Never in debug or test.
@Paberu85
@Paberu85 Жыл бұрын
First stl becoming part of a standard. Now, all of this boost bs. For what? So that more JavaScript devs could feel comfortable with c++?..
Modern C++: Unique Ptrs and Vectors
16:25
Dave's Garage
Рет қаралды 148 М.
Master Pointers in C:  10X Your C Coding!
14:12
Dave's Garage
Рет қаралды 339 М.
黑天使只对C罗有感觉#short #angel #clown
00:39
Super Beauty team
Рет қаралды 36 МЛН
小丑女COCO的审判。#天使 #小丑 #超人不会飞
00:53
超人不会飞
Рет қаралды 16 МЛН
Tuna 🍣 ​⁠@patrickzeinali ​⁠@ChefRush
00:48
albert_cancook
Рет қаралды 148 МЛН
Что-что Мурсдей говорит? 💭 #симбочка #симба #мурсдей
00:19
31 nooby C++ habits you need to ditch
16:18
mCoding
Рет қаралды 849 М.
AI on FPGAs Explained
6:34
beetlebox
Рет қаралды 3,9 М.
Weak Pointers in C++ (std::weak_ptr)
17:25
The Cherno
Рет қаралды 61 М.
E01: Stupid C++ Tricks with Dave
15:43
Dave's Garage
Рет қаралды 342 М.
unique_ptr: C++'s simplest smart pointer
11:54
mCoding
Рет қаралды 47 М.
C++ Super Optimization: 1000X Faster
15:33
Dave's Garage
Рет қаралды 331 М.
7 Outside The Box Puzzles
12:16
MindYourDecisions
Рет қаралды 134 М.
E02: Stupid C++ Tricks: Most Dangerous C Functions (E02)
23:33
Dave's Garage
Рет қаралды 187 М.
黑天使只对C罗有感觉#short #angel #clown
00:39
Super Beauty team
Рет қаралды 36 МЛН