E01: Stupid C++ Tricks with Dave

Ғылым және технология

Retired Microsoft operating systems engineer Davepl reveals some of his favorite C++ tips and tricks accumulated during nearly 40 years as a C/C++ programmer.
Dave was a developer for Windows Task Manager, ZIP Folders, Space Cadet Pinball, the Windows Shell, OLE/COM, MS-DOS, and much more.
Some Microsoft Campus photos are copyright and credit to Ken Sykes, used with permission.
ERRATA: None yet, but if you spot a mistake, please raise it in the comments and I'll update here as appropriate!

Пікірлер: 594

  • @gavtriple9
    @gavtriple92 жыл бұрын

    “I’ve been coding in C and C++ for 40 years… NoW tHat IM fInAlLy gEtTinG gOoD aT iT”

  • @maxemore

    @maxemore

    2 жыл бұрын

    Unfortunately, it's not that far from the truth...

  • @mademax1

    @mademax1

    2 жыл бұрын

    This really got me

  • @user-qg2dg9vu8w

    @user-qg2dg9vu8w

    2 жыл бұрын

    *cries in C#* ...sorry

  • @richardikin

    @richardikin

    2 жыл бұрын

    Same for me, and I'm decent at C/C++. Haven't quite reached 'good' yet 😁😁

  • @sushio4247

    @sushio4247

    2 жыл бұрын

    The fact that he was making windows only makes it funnier

  • @tzisorey
    @tzisorey2 жыл бұрын

    Dave: "Let me tell you about the time I updated a million lines of code with a single commit" My first thought: "What... *_intentionally?"_*

  • @schiang

    @schiang

    2 жыл бұрын

    That's just a bad habit IMHO. But I cannot change even one line in the Windows kernel so you can ignore my opinion.

  • @rowenkylee5627

    @rowenkylee5627

    2 жыл бұрын

    It was several years ago when commit best practices didn't exist.

  • @tzisorey

    @tzisorey

    2 жыл бұрын

    @@rowenkylee5627 I'm not sure that would need a "Best practises" - but I guess "commit" in general was new enough, that maybe "commit common sense" hadn't really come to be, yet?

  • @johndorian4078

    @johndorian4078

    2 жыл бұрын

    i mean, if it was just a find and replace then it's not that impressive. And i could see it making more sense to do it all at once cause it's a no win situation.

  • @yevhenukrainianer4781

    @yevhenukrainianer4781

    2 жыл бұрын

    The rule is to take an update by an update to get it checked easily where it fails.

  • @locusf2
    @locusf22 жыл бұрын

    Having done C++ on/off for 20 years now this was really useful to know!

  • @DavesGarage

    @DavesGarage

    2 жыл бұрын

    Thanks! I was trying to think of stuff that might surprise everyone!

  • @gideonunger7284

    @gideonunger7284

    2 жыл бұрын

    ​@@DavesGarage As a game dev i did know about everything shown here. Messing with memory is my bread and butter after all. But i really like how clearly everything is explained. including what the point of those tricks are and what their advantage is in certain cases. Its great at giving people a peek behind the curtain of c++ I really liked the mention of using alloca if you really know your memory lifetimes. (did that a few times before)

  • @locusf2

    @locusf2

    2 жыл бұрын

    @@DavesGarage my knowledge is mostly from Stroustrups C++-98 version of the book, was quite a challenge to read it as a 16-year-old

  • @kvdrr

    @kvdrr

    2 жыл бұрын

    @@gideonunger7284 Since you're a game dev, I'm curious - did you ever use fibers (instead of mere threads) when working on a game? I'm wondering why aren't these used more widely eversince seeing them referenced as an easter egg in The Sims 4.

  • @MrAbrazildo

    @MrAbrazildo

    2 жыл бұрын

    ​@@kvdrr I heard of fibers as a memory handler, created by the team behind of _Last of Us_ remake. But that aims apps that allocate too much memory, challenging the machine capacity.

  • @timwhitman
    @timwhitman2 жыл бұрын

    "Its not stupid if it works" ... the 1 pearl of wisdom I've learned in 20 years of IT support. Love watching these videos, hearing these stories & hearing all this wisdom in general. It has helped me retrospectively understand a lot of issues I've tried to fix in the past. Thank you for sharing your stories & wisdom, its greatly appreciated!

  • @darksunrise957

    @darksunrise957

    2 жыл бұрын

    The thing is, "it's not stupid if it works" only applies until it stops working.

  • @thisismyalias

    @thisismyalias

    2 жыл бұрын

    @@darksunrise957 No shit..

  • @Denathorn

    @Denathorn

    Жыл бұрын

    "Its not stupid if it works" I wrote a match-3 engine once, and, I had a setup where if you cleared any 3+ items and new ones came down and settled into their positions, they won't cause other matches, thus, no chain reaction where the user just sits there and the game plays itself... Anyway, it was all working great, but once in a blue moon, a match would happen, and I was like "?", so going over the code, I just couldn't see where the bug was coming from, the logic looked sound, over and over I went, printing it off, reading it in bed or even on the bog... Nothing stood out, and because it was so rare in occurrence, it made it harder to debug, I tried everything... And, then, I changed one part of the condition in an If statement, which didn't look right, but had a go anyway, and, it never came back... And I still go over that code in my head and think... What's it doing, because believe it or not... I still don't know how the change worked in terms of the overall logic behind it! :D

  • @RaisingAwesome
    @RaisingAwesome2 жыл бұрын

    I feel like Dave is filling in all mysteries in my life for the last 26 years.

  • @u263a3

    @u263a3

    2 жыл бұрын

    True this.

  • @kylman5001
    @kylman50012 жыл бұрын

    There is nothing better than having someone who is well versed in the nuance of something extremely technical, state in simple terms how something works. Great video and love the channel! Please keep making these!

  • @ferinzz

    @ferinzz

    4 ай бұрын

    And then go on to tell you "This complexity has been solved, please don't go through the pains that I have" Unless you need to do some very specific optimisations.

  • @emmanuelbeaucage4461
    @emmanuelbeaucage4461 Жыл бұрын

    Thanks Dave! I'm an aspie and after a hard depression years ago, I was no longer able to code. I'm not sure why but during one of your quick tips in this video, it's like if something switch in my head... I picked up c++ pocket ref and somehow, it now make sense again!

  • @knyshov
    @knyshov2 жыл бұрын

    Learning from the master is always more fascinating. Thank you. I expect your videos will inspire more people to become systems developers.

  • @SpinStar1956
    @SpinStar19562 жыл бұрын

    Definitely appreciate the C++ deep dives as well as doing the more mundane more efficiently. Thanks Dave!

  • @barc0deblankblank
    @barc0deblankblank2 жыл бұрын

    I'm always humbled and fascinated by your videos, Dave. Keep it up

  • @user-fc8xw4fi5v
    @user-fc8xw4fi5v2 жыл бұрын

    This is easily the coolest YT channel I've ever found. You always post such entertaining, informative, and useful content!

  • @josephcohen734
    @josephcohen7342 жыл бұрын

    I've been wondering how the compiler knows what to do with a the delete[] operator FOREVER. Thanks very much for that.

  • @vedantganesh6923

    @vedantganesh6923

    2 жыл бұрын

    Well it's a question of a system call. It's the kernel's responsibility to keep track of allocated threads, mem, cpu, disk, etc. delete[] basically is your program telling the system "Hey, I'm done with this bit of memory. You can have it back". The C++ compiler need not interfere further than just performing a system call with the pointer as an argument.

  • @josephcohen734

    @josephcohen734

    2 жыл бұрын

    @@vedantganesh6923 I don't think that's right. For one thing, C++ allocators tend to get a large chunk of memory allocated to the program before it even starts main. When you call new or even malloc it can then usually find space in that pre allocated block for what you need. More importantly, the delete and delete[] operators both call the destructor, which the system doesn't know anything about.

  • @kvdrr

    @kvdrr

    2 жыл бұрын

    ​@@vedantganesh6923 ​ Absolute non-sense explanation, you're confusing syntax stuff with actual implementation. LOL

  • @Mozartenhimer

    @Mozartenhimer

    2 жыл бұрын

    I'm pretty sure when you'd grab memory, the size of that memory is stored somewhere, hence why you can just free a pointer, with no size attached. So if the compiler can read that value, it can determine how many elements are in the array.

  • @SianaGearz

    @SianaGearz

    2 жыл бұрын

    @@Mozartenhimer The compiler? No. The size of the memory behind the pointer is stored within the malloc() implementation, which is part of some underlying standard library - on Windows, it's the MSVCRT, the C runtime of the Visual C and C++ compiler that is linked in automatically, on desktop Linux, it's the glibc or eglibc, on embedded systems, something like Newlib. On FreeBSD, malloc is implemented by jemalloc library. For compatibility reasons, and to avoid more bloat than necessary, default C++ operators like new/delete generally leverage the underlying C implementation. At least this used to be the case, now it's got obscured behind layers of stuff, because there is now aligned allocation in C++, which means it must be preconfigured to use some malloc which offers this feature, where you can specify the alignment granule per allocation, and it might or might not be the same as used by C on the same system, and it will generally turn out to be, but not using the same standard C API. Sorry, maybe a bit of a digression. It is however not a syscall, as that would be insanely slow. malloc uses syscalls to request pages from the operating system, so chunks of memory that are 4kb in size or multiple of that. To figure out what size information malloc actually stores, you need to consider the interface. There is three functions, malloc, realloc and free. None of these allow you to query the size of allocation. Crucially, malloc has no need to know the number of bytes that you requested, just the number of bytes that it returned. Most modern implementations are derived in one way or another, often more ideologically than in implementation, from Doug Lee's dlmalloc, which is a bucket system. So it has buckets of a number of sizes under a page in size, so let's say 16, 24, 32, 48, 64, 128, 256, 512, 1024 etc - it will have all the power-of-two sizes and can have a bunch of useful interim sizes. Each bucket is a contiguous chunk of memory that stores allocations of that given size, so for example a bucket that contains 128 elements of size 32 exists, and it's 4kb capacity in total. When a malloc request comes in, so you say malloc(28), it rounds it up to 32, and goes to the pool of buckets with 32-byte elements, and finds the first bucket that has some free space in it, and returns the address of that element, and marks that space as used in the bucket's bitmap. If no suitable bucket exists, it makes a new one by requesting a page from the operating system. So as far as malloc is concerned, the capacity of that allocation that you requested as 28, is really 32. It will store the addresses of buckets in some sort of tree, and it will walk that tree to find the address, and it will find the bucket it belongs to, and hey look that's a bucket with 32-byte elements. And if you ask to "free", it will just edit the occupancy bitmap to indicate that this slot is free in the bucket, and if the bucket was at that point fully occupied, it will re-add the bucket to the pool of buckets with space available. If you request to realloc, if the new size is under 32, it might as well leave the allocation just where it was and do nothing. Otherwise, you should be able to imagine what needs to happen to satisfy a larger reallocation request. So ultimately, malloc doesn't generally know the original allocation size, just some sort of upper-bound approximation of it.

  • @zoso25
    @zoso252 жыл бұрын

    It's quite serendipitous that I come across placement new in your video, having asked about it in an interview just the other day! I thoroughly enjoy your videos and always try to glean off as much as possible from them. I wish I had such insights when I was starting out with C++ programming.

  • @jrlanglois
    @jrlanglois11 ай бұрын

    All of this new operator stuff is common in game engines. Cool to see it covered succinctly AND in the most straightforward way possible. Awesome!

  • @JeffHykin
    @JeffHykin2 жыл бұрын

    This format, the stories with tips in between, is fantastic. I wasn't expecting much from the tips, but I was happily surprised/impressed. It is very apparent your skill is on an entirely different level than other senior devs I know of. I hope you'll cover some of the more niche C++2020 topics some time.

  • @asdasfdfgewqgrgyjh
    @asdasfdfgewqgrgyjh2 жыл бұрын

    Keep up the great work. Glad youre spending you time making interesting videos

  • @androidrandom9979
    @androidrandom99792 жыл бұрын

    More like this! Could gather 'round and listen for hours

  • @MrAbrazildo

    @MrAbrazildo

    2 жыл бұрын

    hehe, I love tips and tricks from the best language ever.

  • @gabrielvilasboas1038
    @gabrielvilasboas10382 жыл бұрын

    These are not stupid tricks, this is pure knowledge. Thank you for sharing.

  • @ast_rsk
    @ast_rsk11 ай бұрын

    As a self-study individual who primarily works professionally in C# with no formal training and is trying to learn C++, sprinkling in "tidbit" videos like this has been helping me remain focused while expanding and equating the basics with some more real world samples. Thank you for all these great resources! I would love to hear your thoughts on how to improve studying for someone in my position.

  • @bobfish7699
    @bobfish76992 жыл бұрын

    Would love to hear the inside story of the Cairo project. I remember the hype at the time...

  • @paulgray1318

    @paulgray1318

    2 жыл бұрын

    I remember that BYTE magazine that featured that.

  • @goncalo-LX
    @goncalo-LX2 жыл бұрын

    Fantastic! 5:58 the new OS is borning... I love that (disguised) passion of yours talking about the creation of what you love and make... Congratulations!

  • @AJSquirrel53
    @AJSquirrel532 жыл бұрын

    This was awesome! I really enjoy this amount of complexity and detail. I learned a lot but it was still understandable

  • @OmegaNineProfile
    @OmegaNineProfile2 жыл бұрын

    I always start these videos watching it on a second screen while playing a game. I end up pausing the game, staring to the right screen and then opening up Google over my game to search for tidbits you mention. Thanks for making me a single task person for 15 minutes a week.

  • @metallitech
    @metallitech2 жыл бұрын

    Great presentation. I'm looking forward to watching more of your videos.

  • @whatsyoursteezo
    @whatsyoursteezo2 жыл бұрын

    I have been doing C/C++ for just over 22 years and love this!

  • @IsusaWH
    @IsusaWH2 жыл бұрын

    This is without a doubt my favourite channel

  • @RetroTechChris
    @RetroTechChris2 жыл бұрын

    Dave, you are an amazing instructor! And, having been a Java guy for so long now, I realized just how much C++ I had forgotten... and I had never thought about overloading new and delete before, mind blown. Looking forward to the next video!

  • @FoxSlyme

    @FoxSlyme

    2 жыл бұрын

    In Java you would need a private constructor and then make a factory method to achieve similar effect?

  • @a.j.s.3979
    @a.j.s.39792 жыл бұрын

    The placement new was a useful refresher for me (28 years C/C++). Loved the million lines of code story, look forward to that bigger episode. The 'name in the header makes you the owner' issue is a classic :)) I've had to deal with a fair share of disbelief about my lack of knowledge of a file at times.

  • @rikschaaf

    @rikschaaf

    2 жыл бұрын

    Also happened to me once when I had to copy the code of a module to a new package in java. Git regarded them as new files, so put my name in the git blame XD

  • @DavesGarage

    @DavesGarage

    2 жыл бұрын

    Thanks for sharing!

  • @MorganC-zz1bg
    @MorganC-zz1bg2 жыл бұрын

    Great stuff Dave, would love to see more like this.

  • @BinaryReader
    @BinaryReader2 жыл бұрын

    Dude, you're so experienced in the dark arts of C++. These vids are so interesting to watch and hear the nuance of all these low level features. Kudos man.

  • @MrJugsstein
    @MrJugsstein2 жыл бұрын

    So far above my head as Im looking for O2. But I really enjoy your insights stories and cool delivery. Tks for your time Will

  • @ShankingDisaster
    @ShankingDisaster4 ай бұрын

    i hope you know I literally grew up and fell in love with using everything you created. I can't believe I found the guy responsible for creating just about every useful big windows tool

  • @scottanderson2871
    @scottanderson28712 жыл бұрын

    Always learning something new from your vids!

  • @Argletrough
    @Argletrough2 жыл бұрын

    I'm a casual tinkerer and I've gotten it into my head that 'I don't need all this fancy C++ abstraction nonsense.' Hopefully this series will help me with understanding when these approaches can be applicable, in the *real world!!!* I didn't realise you could overload new and delete; that example was clear and informative. Interesting video, thank you!

  • @seangraves3509
    @seangraves35092 жыл бұрын

    Thanks Dave, 30yrs+ and still learnt something new after watching this video. As they say "Every Days a School Day" 👍 Bet that was a nervous moment checking in all those changes with a "What if and Have I" moment 😂

  • @ElJohnerino
    @ElJohnerino2 жыл бұрын

    I'm not a programmer but I can still follow along with the words and the logic. It's strangely peaceful. Thanks, Dave.

  • @johnrevill9592
    @johnrevill95922 жыл бұрын

    Thanks for another great video Dave. As an old Pascal/Delphi programmer, having only moved to the C++ realm in the last 10 years or so for microcontroller programmng, this was very interesting.

  • @rowenkylee5627

    @rowenkylee5627

    2 жыл бұрын

    Pascal/Delphi still exist?!

  • @overclucker
    @overclucker Жыл бұрын

    I'm just starting to learn C++, again. I'm liking this series because it helps me shift out of C thinking.

  • @richardclarke376
    @richardclarke3762 жыл бұрын

    Practical and fascinating! Thanks Dave

  • @BlueSheep95
    @BlueSheep952 жыл бұрын

    The more I know about C++, the more I know that I don't know anything about C++...

  • @mr_noodler
    @mr_noodler2 жыл бұрын

    I love how he says that after 40 years he's finally starting to be good at it! This is a fantastic programmer!!!

  • @thomasandrews9355
    @thomasandrews93552 жыл бұрын

    Love these types of videos! More!

  • @NeilRoy
    @NeilRoy2 жыл бұрын

    Funny, when you were describing the different RAM types, I was thinking "this sounds like my old Amiga RAM" and then you mentioned the Amiga as an example. Loved that computer.

  • @mateusv
    @mateusv Жыл бұрын

    40 years as a C/C++ programmer, and you are finally getting good at it... well, good for you! For some reason, that makes me think I should pursue another career hahaha

  • @RA-NAF
    @RA-NAF Жыл бұрын

    Great video. I've learned new things about C++ thanks to that (things that really nobody talks about).

  • @drop0ne_f20
    @drop0ne_f2021 күн бұрын

    This was very helpful and educational for me.

  • @gerudobombshell
    @gerudobombshell2 жыл бұрын

    Very cool stuff! Thanks for the awesome tips!

  • @indi__
    @indi__2 жыл бұрын

    pretty much all of this went over my head, but it was a joy to listen to. hopefully i'll learn enough C++ one day to know what's going in at least the first 5 minutes of the video

  • @sanjitdaniel4588
    @sanjitdaniel45882 жыл бұрын

    Thank you!!! Very informative and entertaining!

  • @hexen93
    @hexen932 жыл бұрын

    As someone with ADHD, the pace of these videos is great! I was captivated the entire time and learned a nice handful of new info. Thanks a bunch for these videos :)

  • @Beatsbasteln

    @Beatsbasteln

    2 жыл бұрын

    as someone with ADD i found it a little slow at the middle part when i started getting impatient about when exactly he'd mention smart pointers

  • @davak72

    @davak72

    2 жыл бұрын

    As someone with ADHD, I’m 4 minutes in, very engaged, but also reading the comments haha

  • @robsku1

    @robsku1

    Жыл бұрын

    @@davak72 Ha - sometimes happens to me as well; but I try to force myself to focus on the video alone when I really want to get all of it, because I've noticed that no matter how it feels like I can focus on both the video and the comments, it usually doesn't work that well in reality and I'll end up missing half the video :D Oh yeah, I forgot to say I'm writing this as someone with ADHD ;D I'm betting there's plenty of us here ;)

  • @TheCocoaDaddy
    @TheCocoaDaddy2 жыл бұрын

    Great video! I think it's awesome you ported explorer to Windows NT on MIPS. :) Thanks for posting!

  • @michelle_77
    @michelle_772 жыл бұрын

    God I love these videos. Thanks Dave.

  • @ronen124
    @ronen1242 жыл бұрын

    I am not a c++ developer yet I liked your deep understanding of the subject as well as the interesting stories behind the code

  • @TheExcellentVideoChannel
    @TheExcellentVideoChannel2 жыл бұрын

    On the money Dave. More of this please.

  • @u263a3
    @u263a32 жыл бұрын

    Dave is sooooo cool ! He's like the computer geek dad you've always wanted

  • @N....
    @N....2 жыл бұрын

    4:30 actually the std::size function does this already, no need to write any macros or templates anymore.

  • @ahmedinelec

    @ahmedinelec

    2 жыл бұрын

    would it even work for a dynamically allocated array ?

  • @kwkfortythree39

    @kwkfortythree39

    2 жыл бұрын

    @@ahmedinelec Yeah, you have std::vector::size()

  • @ahmedinelec

    @ahmedinelec

    2 жыл бұрын

    @@kwkfortythree39 im talking about a new* allocated one

  • @kwkfortythree39

    @kwkfortythree39

    2 жыл бұрын

    @@ahmedinelec I now, my suggestion was to avoid using those unsafe constructs and use std::vector.

  • @jackoberto01

    @jackoberto01

    2 жыл бұрын

    @@kwkfortythree39 While that works and is preferable in most situations there's still some situations when you doing low level code and normal array would be all you need Vectors and other containers are convenient but come with some overhead and functions that you might not need

  • @laviljoen
    @laviljoen2 жыл бұрын

    Love your style Dave... have you thought of creating a C++ programming training course taking someone from beginner noob to super advanced leet coder. You have so much experience and knowledge as well as a natural ability to convey complex concepts effortlessly... a CS course by you will be a win for the whole industry.

  • @paraxicgaming5743

    @paraxicgaming5743

    2 жыл бұрын

    gonna second this, there are things about C++ that dissuade me from using it and staying with C, having the mystery taken out of it would go a long way towards converting me

  • @Denathorn

    @Denathorn

    Жыл бұрын

    I agree... I mean, I know C++, but, as a tinkerer, I never keep up with the standard, like, his explanation of "new" was great, because, I would never of went into it far enough to learn that... And, well, now I know, and he put it across so well too!

  • @kaanx4464

    @kaanx4464

    Жыл бұрын

    i dont think that leet coder are good programmer, but that is just my opinion. Maybe you know your data structure and some algorithms for it, but that's it. Dave could teach how to really program some good software, that would be a nice course

  • @yiannishadjiyianni7737
    @yiannishadjiyianni77372 жыл бұрын

    Great video. Please more like these!

  • @nice8D
    @nice8D2 жыл бұрын

    It was a little advanced for me, i've mostly worked in C#, php, and JavaScript so some of the C++ pointer syntax go's over my head but I was still able to learn something about better memory management and tools I could use to do so. I enjoyed this.

  • @technowey
    @technowey4 ай бұрын

    .Thank you for your excellent videos.

  • @markpavel5552
    @markpavel55522 жыл бұрын

    Very much thanks this was very useful to know for a big portfolio project

  • @FufuFang
    @FufuFang2 жыл бұрын

    This was super useful! Thanks a lot!

  • @officeboyreloaded5427
    @officeboyreloaded54272 жыл бұрын

    Thanks Dave, it's a pleasure !

  • @kellymorgan2273
    @kellymorgan22732 жыл бұрын

    I learned more about C/C++ from this one video than I ever learned in school.

  • @dertseha
    @dertseha2 жыл бұрын

    Wow, I really forgot about the placement new operator. I can't remember to potentially have had an immediate need for it - yet it feels as if a possible option was missed. Thank you!

  • @scialomy
    @scialomy2 жыл бұрын

    The Standard Library is part of the C++ language. Unless you are stuck in 1994, please use it. See std::min (1:28), std::size (15:42).

  • @viktor.martensson
    @viktor.martensson2 жыл бұрын

    Love these :-) learned C++ in the early 90s, first real challenge in my career was to port a win16 app to 32 bit. One 64loc file with half of the lines in a single function with nested switch statements. :-)

  • @unclerojelio6320
    @unclerojelio63202 жыл бұрын

    I’ve played with C/C++ for nearly 30 years but never really deeply. Although I understood all the terms and concepts here I’ll probably never need to use them. Still, I enjoyed this video and hope you keep more like these as well as your LED videos coming in the future.

  • @SnijtraM
    @SnijtraM2 жыл бұрын

    An interesting thing about placement new is that you can construct individual elements of an array using parameters if you want. But it is a bit involved. I've done it once or twice. Be sure to call their destructors too when you're done.

  • @Mozartenhimer
    @Mozartenhimer2 жыл бұрын

    It really goes to show you know what you're talking about when you describe detailed knowledge of C++ stupid. It shows a respect of when to use it, and knowing that you probably never really want to.

  • @JakePomperada
    @JakePomperada2 жыл бұрын

    Thank you so much Dave

  • @davea136
    @davea13625 күн бұрын

    This was great fun! Thanks Dave! (Also, never change the interface declared by a spec. Don't override new ro catch bad-alloc and return null. This will bite you some day in the future.)

  • @torinireland6526
    @torinireland65262 жыл бұрын

    Speaking of stupid C++ tricks, I'm suddenly reminded of the time I wrote a template class which used void pointers to automatically transfer the contents of one variable into another variable (regardless of type) with no safety checks whatsoever. Think of it sort of like a type cast - but way, way more dangerous and amateurish. The funniest part is - I can't even remember what I wrote it for in the first place! Probably to make some spaghetti code work instead of completely rewriting it, knowing me. Y'all should be very glad I didn't write (any part of) your operating system! It would likely be an awful unmaintainable mess, LOL.

  • @TotalSundae
    @TotalSundae2 жыл бұрын

    I wish you were my OS teacher when I took it. These tips are great!

  • @jamescache1768
    @jamescache17684 ай бұрын

    This guy rules. If only I could keep up with his super fast manner of speech! Each time he's explaining sth I need to rewind several times to actually understand the idea behind what he's saying. It's like reading technical reference vs reading a popular magazine. While you may just briefly look through the latter and catch the idea conveyed by the article, the former requires a lot more diligence at reading in conjunction with a serious thought process to meaningfully consume each paragraph and understand the many important details it reveals. More often than not, you'd have to read a sentence twice or even thrice.

  • @colinmaharaj
    @colinmaharaj Жыл бұрын

    Doing C/C++ for 30 years, yeah, I'm still learning a few things

  • @nathanielbass1315
    @nathanielbass131510 ай бұрын

    WE LOVE YOU DAVE!!

  • @ldohlj1
    @ldohlj12 жыл бұрын

    This guy edits in 1080p in the style of the 90's edit style. I love him

  • @user-uz5ih8bp8c
    @user-uz5ih8bp8c5 ай бұрын

    I'm impressed! The closest thing to that I ever did was rebuilding the OS on a CDC 1700 when I was totally stoned. You have to think carefully before hitting enter when you do that.

  • @mumakin1
    @mumakin12 жыл бұрын

    So glad you threw in the cpp11 smart pointers at the end. These days I get a little scared when I see the new operator :)

  • @johnshaw6702

    @johnshaw6702

    2 жыл бұрын

    I've rarely used new over the years; I avoid it when possible. You can write complete liberties depending on the STL without using it once directly. Anytime I see new used directly, I start looking very closely to see (1) why they used it and (2) how they are ensuring the memory is released. Smart pointers are the way to go.

  • @rowenkylee5627

    @rowenkylee5627

    2 жыл бұрын

    Smart pointers have the disadvantage of being slower. It depends on the implementation and the compiler used. The Clang compiler do a good job optimizing smart pointers. But it's always best to decompile the binary and check.

  • @patricksteffanic5491
    @patricksteffanic54912 жыл бұрын

    Your shirt today is such a winner!

  • @freelancer42
    @freelancer422 жыл бұрын

    "Thanks to C++ inheritance it's a little more complicated than that." If I had a nickel for every time...

  • @mitikox
    @mitikox2 жыл бұрын

    Coming from the .NET ecosystem and on my journey towards code optimization, I found this information quite intriguing and important! Thanks for the video!

  • @rowenkylee5627

    @rowenkylee5627

    2 жыл бұрын

    Code optimization you say. If you need speed specialized assembly (ASM) functions is the way to go. Compilers still do stupid stuff that cause slow downs. Especially Microsoft's visual studio compiler. It's infamously bad. Many compiler bugs Microsoft refuse to fix. As usual.

  • @EriComicuDesu
    @EriComicuDesu2 жыл бұрын

    Just starting to learn C++! Very interesting video!

  • @IntenseGrid
    @IntenseGrid2 жыл бұрын

    I appreciate the reference to Amiga architecture as I have been studying this lately. When I had an Amiga I wasn't nearly up to speed about how these things worked. I am thinking about getting an Amiga and getting into programming it as they were way ahead of their time, and it is simple enough that I can completely understand the whole thing...unlike modern PC's. The alternative is the Parallax Propeller 2, which is quite a chip that you might like. It has 8 cores, each with 4K of memory and they share 512KB of high bandwidth memory. It is not as straight forward as programming an ESP32 with it's libraries, but with 64 IO's each capable of doing D2A and A2D, it's quite a capable chip. It does 350MHz overclocked, and some have written drivers for 4bit PSRAM's to get up to 350MB/sec burst out of 8 of them, which is fast enough for fairly high resolution screens. There's even an emulator of the Sega MegaDrive that internally emulates the 68000 and the z80 in seprate cores. You can find it on Github under "MegaYume". It would be quite interesting to see what someone with your intellect could do with such a chip.

  • @jimwinchester339
    @jimwinchester3392 ай бұрын

    14:20 I worked on more than one C/C++ compiler, including very early DOS-hosted ones (before MS bought Lattice's), and the first 808x-native C++ compiler (predecessors all used Cfront). This meant I maintained my own run-time library, and toward the end, this included code for [global] operator new & delete. When I added the array versions, I just followed the paradigm used in the heap design, which was to record the length of the block, and then just return the address of the next word to the user (after invoking the default ctor for each element). This meant we were *absolutely* dependent of the user releasing using the corresponding operator delete[], so that we could back up over the count before commencing with the element-destroying loop.

  • @OldePhart
    @OldePhart2 жыл бұрын

    Good morning Dave. Glad you and Yogi went your separate ways without incident. .

  • @urmeli0815
    @urmeli08152 жыл бұрын

    Thumbs up for mentioning Amiga's ChipRAM and FastRAM :)

  • @Denathorn
    @Denathorn Жыл бұрын

    You know... MS should have had you doing this stuff while you worked there, because when you see "Windows", it is what it is... Windows, you tend to forget that there are people behind the OS, and they have reasons and decision making to do which we as users may go "What have they done that for?". I know they have developer portals, with videos and such, but these are aimed at professionals in the most part, so it is refreshing to see videos like this on average joe platforms like youTube where us programming tinkerers can see it... Bit of cocktail music in the background, explaining in layman's language, drop of humour, all the while, popping up little sample code that makes sense, all bundled with little stories from days gone by. I like it! 👍

  • @UAVXP
    @UAVXP2 жыл бұрын

    Nice! Very interesting, keep it going please)

  • @alexandrohdez3982
    @alexandrohdez39822 жыл бұрын

    Really nice video 👏👏👏, long live C/C++

  • @toerti9589
    @toerti95892 жыл бұрын

    I remember Windows Programming 5th Edition (i think) by Charles Petzold, and the excitement that came with the change to unicode. The future looked glorious in those days, good times.

  • @WizardOfArc
    @WizardOfArc2 жыл бұрын

    “Pucker factor” nice term

  • @sanfords
    @sanfords2 жыл бұрын

    Gad. I had to port the icon code from win95 to NT and the caching tricks took me quite awhile to grok and just before I was to check in 2 months worth of work, my HD crashed and I had not backed up the data. (SLM SUCKED) That got me fired from Systems unfortunately. I also did a lot of header file cleanup and the Win 16-32 porting layer so I can relate to getting your name on tons of files while not really knowing too much about them. Many years later I got interviewed for the Messenger team and they thought I was a god because my name was all over the NT header files. Love your reminiscing.

  • @J.D-g8.1
    @J.D-g8.12 жыл бұрын

    Very useful! The mix of obvious/kindergarten stuff with deeper stuff works well. I consider myself fairly dumb as well, so in general nothing can be too simple; or that is- explained to simply at least. :)

  • @photonrayleighm2666
    @photonrayleighm26662 жыл бұрын

    I really enjoyed this! I learned C++ a while ago and haven't used it much since, but I've been wanting to get back into it. The nuances you covered in this video are really valuable, as I don't see them covered a whole lot elsewhere (at least, not in an easily digestible format). This video worked sort of as a refresher for me and taught me new things.

  • @dimasveliz6745
    @dimasveliz67452 жыл бұрын

    Awesome man!

  • @casperes0912
    @casperes09122 жыл бұрын

    Very helpful. I've written a lot of C and very very little C++, so really took a lot with me here. Seems scary to override new with a version on the stack instead of the heap though. Now when someone comes along, makes a "new whatever" and expects it'll live beyond the scope, it'll be a debugging nightmare

  • @Smaxx

    @Smaxx

    2 жыл бұрын

    Guess that's why there was the remark about it being for some internal class that's never returned outside (so possibly not even visible/accessible from the outside).

  • @DavesGarage

    @DavesGarage

    2 жыл бұрын

    Yep, it was clever but not something I'd suggest because the caller has to know. That's why I said "not judicious" I guess!

  • @jessicaclarke7820

    @jessicaclarke7820

    2 жыл бұрын

    I don't see how this could possibly work though? The memory returned by _alloca has automatic storage duration so its lifetime ends when the calling function returns. If you overload operator new, the calling function in question is operator new itself, so the lifetime is until the end of operator new, meaning the pointer cannot be used in the function that called new, as that would be a classic use-after-(implicit-)free. Accessing the memory (object) is undefined, and the value of the pointer to that memory itself is even indeterminate (C99's wording; C++ is slightly different but essentially the same core idea); see things like the WG21 "pointer zap" proposal for providing concrete semantics of this. I could only see it working if you in fact changed the caller to use _alloca and placement new?

  • @Mathhead2000
    @Mathhead20002 жыл бұрын

    This was excellent. I didn't know about alloca(), or new (ptr)

  • @ImTheTuffGuy
    @ImTheTuffGuy2 жыл бұрын

    Awesome! Would love to hear about safe pointers too.

Келесі