Memory safety in C++, Zig, & Rust (part 1)

Sdílet
Vložit
  • čas přidán 1. 07. 2024
  • Part 2: • Reference Cycles in Ru...
    Code: github.com/contextfreecode/sa...
    0:00 Intro
    0:27 == C++ (cpp) ==
    1:30 Returning expired memory
    3:09 Pointers and values
    4:02 Analyzing memory errors
    6:09 Secret frees
    7:19 Out of bounds
    8:31 Initializing values
    8:50 == Zig ==
    9:33 Initializing values
    9:50 Out of bounds
    10:13 Explicit destruction and memory leaks
    11:26 == Rust ==
    12:13 Trying to return locals
    13:00 Trying to ignore secret frees
    13:50 Trying to set parent pointers
    15:27 Out of bounds
    15:54 Outro
  • Věda a technologie

Komentáře • 227

  • @vintagewander
    @vintagewander Před rokem +361

    My man just trying to mess with rust and the compiler be like I'm 10 dimensions ahead of you

    • @dyslexicsteak897
      @dyslexicsteak897 Před rokem +28

      Yea this is the thing people complain about you can't really mess with Rust. I've reached a point of understanding what the compiler wants but if you don't then you will have a hard time trying to just throw things together.

    • @vintagewander
      @vintagewander Před rokem +27

      @@dyslexicsteak897 I think what most people struggles with rust is that because the language is just different, it throws people off right away since they've gotten too used to the way that they normally program applications.
      I've also used rust for a while and despite that it forces you to have a different mindset, which is what makes the learning curve of rust so steep, but then you just kinda know how to use it as time goes by, accepting None and Err variant to be exists along with the "happy path" that you trying to program
      And another thing that I can feel about using rust is that when you get back to program in other languages, you felt something is missing, you just felt that this thing might be throwing an error at anytime but the compiler doesn't tell you, you just feel "unsafe" and "unsure" about what you're doing and what the compiler will do, because the language is designed to make you program the way you want, and not the way that the computer want, in which errors are everywhere
      It's basically like switching from vscode to neovim sort of feeling, it felt hard at first, but as you get used to what the benefits of the alternative offers, it really does feel weird switching back

    • @dyslexicsteak897
      @dyslexicsteak897 Před rokem +7

      @@vintagewander yea trying to use anything else is difficult now

  • @shimadabr
    @shimadabr Před rokem +261

    That's the best showcase of Rust safety compared to other languages I've seen so far.

    • @vladimirkraus1438
      @vladimirkraus1438 Před rokem +19

      It may be safe. But it is soooo annoying!

    • @shimadabr
      @shimadabr Před rokem +44

      @@vladimirkraus1438 I had so many frustrations with bugs in C and C++ that seeing "Rust's way" is refreshing. So far I only fighted the compiler on little toy programs. I bet on larger projects it's way more annoying haha

    • @mrlordbrutish
      @mrlordbrutish Před rokem +16

      @@vladimirkraus1438 To be fair, cyclical references is intrinsically one of the hardest things to do in Rust. I believe the std implementation of things like linked list actually use unsafe for this.

    • @someonespotatohmm9513
      @someonespotatohmm9513 Před rokem +15

      @@mrlordbrutish There are things like linked list that as far as i know can't be done in safe rust. But that is why unsafe exists, create support for the link list, place it behind a safe api, and the rest of the language can remain safe. safety by default.

    • @cat-.-
      @cat-.- Před rokem +22

      @@vladimirkraus1438 just like seatbelts. so annoying , but I get to live

  • @xelaxander
    @xelaxander Před rokem +30

    God damnit! You can't leave us dangling here.

  • @danielcarloschavesboll5156

    God damn, nice showcase, waiting for the part 2. Love your videos man, keep it up.

  • @KonstantinUb
    @KonstantinUb Před rokem +52

    Damn, ending on a cliffhanger! And just when it was getting spicy (rusty). Looking forward to part 2!

  • @sporefergieboy10
    @sporefergieboy10 Před rokem +53

    Explainin Rust tree traversal spans multiple videos. Poetic

    • @Dorumin
      @Dorumin Před rokem +5

      That is the true rust way

    • @stevenhe3462
      @stevenhe3462 Před rokem +5

      Not really, if you brainlessly tuck Arc::new(Mutex::new(sth)) everywhere and call it a day.

    • @Dorumin
      @Dorumin Před rokem +6

      @@stevenhe3462 I think in this case a good old Rc could do the trick, since there's no thread stuff going on anywhere... uh, maybe a RefCell

    • @contextfree
      @contextfree  Před rokem +18

      Yeah, part 2 for Rust is Rc.

    • @contextfree
      @contextfree  Před rokem +3

      Other ways to go could be either unsafe or showing ways to avoid feeling a need for parent pointers. Or maybe other approaches I'm not thinking of.

  • @tango2olo
    @tango2olo Před rokem +24

    Rust is the other extreme it seems. Zig looks promising as it follows the middle path.

  • @sodiboo
    @sodiboo Před rokem +17

    in Rust, the standard library does have some ways to represent this data structure. You can make each node (std::rc::Rc), and then represent the parents as another Rc, causing a memory leak (because cyclic strong reference), or even better you can represent the parent ref as a std::rc::Weak, which won't cause a memory leak.
    But this also has its downsides; since it's no longer possible to say that a single node owns another (Rc models shared ownership) you can't guarantee unique mutability; so you're just not allowed to mutate them ever, which is not that great, because this means that you can't set a node's prent after it is a child, or vice versa. But you could use RefMut to explicitly model "dynamically checked references", which does the job of the borrow checker but at runtime. so to truly model the same parent/child relationship you need "children: Vec, parent: std::rc::Weak".
    The reason I'm careful to fully write out std::rc::Weak's name here is because std::rc as well as RefMut are not threadsafe; which is something to think about (though Rust also won't let you do horrible things here, because even in single-threaded scenarios the compiler statically ensures nothing would go wrong when multithreading), and the solution is simple, just change it to "children: Vec, parent: Vec", which is more idiomatic Rust because, well, it's threadsafe, which doesn't mean exactly the same thing as in other languages. personally i love that Rust does give you the option for these dynamic checks in a way that's structurally sound without any opportunity for memory unsafety bugs, without making that the default, it doesn't hide the complexity required to model a certain data structure, you have to explicitly opt in to the additional overhead so you can do additional things

    • @linkernick5379
      @linkernick5379 Před rokem

      It seems GhostCells can help here. Need to check it myself though.

    • @mypersonalstuff-cy7gi
      @mypersonalstuff-cy7gi Před 9 měsíci +1

      please write a blog post, this is too much quality writing for a youtube comment. It is not allowed :-)

    • @sodiboo
      @sodiboo Před 9 měsíci

      @@mypersonalstuff-cy7gi I've considered this, actually. A while back I got similar advice when posting to Twitter (and to anyone who wants to nitpick the name change, I've never used "X" in any meaningful capacity), and considered that it may be useful. Although I guess I could write a "proper" blog post on this topic, I feel that the comment in its current state feels low-effort for a blog post. Maybe I can start a blog with like, a subsection of "micro posts" that are basically just high-quality youtube comments, like this one, with less of an independent structure.

  • @greyshopleskin2315
    @greyshopleskin2315 Před měsícem +1

    Really good video. I hope you keep uploading nice things! Subscribed

  • @d0m96
    @d0m96 Před rokem +3

    Awesome video and start to a new series. Looking forward to part 2 :)

  • @Speykious
    @Speykious Před rokem +3

    Holy shit. I love how you manage to find every possible memory safety error that Rust makes, one un/commented line at a time.

  • @friend7120
    @friend7120 Před rokem +11

    I can't wait to see how this would look in ROC (one day 😅)! the ROC people also have a pretty interesting case study of using both Rust and Zig, actually because of relative memory safety and capability

  • @bekkur81
    @bekkur81 Před 9 měsíci +1

    Very informative video. Thank you for this!

  • @michaelsohnen6526
    @michaelsohnen6526 Před rokem +3

    Integer index to vector. Works every time.

  • @JhoferGamer
    @JhoferGamer Před rokem +12

    i like how concrete zig is

    • @genderender
      @genderender Před rokem +4

      Even learning it just feels like it makes sense, even down to pointers which I always struggled to grasp with C++. Really cool language overall

  • @antronixful
    @antronixful Před rokem +4

    The chad zig. I hate the way rust is passive aggressive with you, and cpp, well...

  • @marknefedov
    @marknefedov Před rokem +11

    Aaah, the cliffhanger

    • @contextfree
      @contextfree  Před rokem +3

      If you want a sneak peek, I do have the code already linked in the description. But the video should be more fun than just looking at code, I imagine.

  • @up4life108
    @up4life108 Před rokem +7

    Hey, not quite related to this video but i was wondering if you will be covering the Roc programming language in any of your future videos. I'm sure you have already have seen it and it developed quite fast and good (in my opinion) this year. It's one of the more interesting languages in the upcoming years i think

    • @contextfree
      @contextfree  Před rokem +1

      I agree with your assessment of its importance, and I do hope to cover it in the next few months. I've toyed with it just a little myself so far. I'm always so behind on everything, sadly.

    • @up4life108
      @up4life108 Před rokem +1

      @@contextfree I'm looking forward to your video! I think it could be interresting to have Richard Feldmann, the designer of Roc, on the video as well. Take your time, the language is still very much unknown, i can wait another few months for a video on it, maybe even more interresting new features have been added by then.

  • @vladimirkraus1438
    @vladimirkraus1438 Před rokem +46

    Me: Meh, such a simple thing as a node tree... it's easy. I know very well what I am doing.
    Rust: No, you don't, you stupid.

    • @contextfree
      @contextfree  Před rokem +26

      Well, I was purposely causing as much trouble as possible. But it's clear from security alerts that this happens easily in real (usually more complicated) code frequently enough as well.

    • @itellyouforfree7238
      @itellyouforfree7238 Před rokem +6

      exactly, you don't. having children/parent pointers intrinsically unsafe because stuff can be moved around and then those pointers would be invalid. rust prevents you from doing this thing in SAFE rust because safe rust GUARANTEES that everything you can do has to be safe. if you can PROVE that in your particular case those intrinsically unsafe operations are fine ("sound"), then you can implement it with unsafe rust the exact same way you would do in C/C++, nothing more nothing less. there isnt any pointer magic that you can do in C and cannot do in rust. the difference is that to do intrinsically unsafe stuff in rust you have to mark it with, well.., unsafe. period

  • @davidrichey2034
    @davidrichey2034 Před rokem

    Great video

  • @regbot4432
    @regbot4432 Před rokem +5

    You brought memories when I wanted to try this new cool rust thing everyone is talking about with some toy programs. This language is literally problem in itself sometimes haha. I don't how much time you need to spend to be fluent.

    • @peter9477
      @peter9477 Před 9 měsíci

      Depending on your background and effort invested, I'd say count on 3 to 9 months to become very fluent in Rust, if not "expert". That's assuming you're actively using it for real projects every week, maybe every day, not just dabbling. (Edit: oh, and it's well worth this investment!)

  • @yash1152
    @yash1152 Před 3 měsíci +1

    5:24 sanitizer options for compilers

  • @mgetommy
    @mgetommy Před rokem

    Good video 👍

  • @TheNoirKamui
    @TheNoirKamui Před rokem +3

    It has got to be Weak pointers to the parent. This tripped me up once for a weekend lol.

    • @contextfree
      @contextfree  Před rokem +1

      Got those coming in the next video, too!

  • @Gordonfreems
    @Gordonfreems Před rokem +34

    This made me want to learn Rust even harder, very cool video

  • @ingmarfalk3306
    @ingmarfalk3306 Před rokem +3

    subscribed

  • @jokinglimitreached1503
    @jokinglimitreached1503 Před rokem +5

    more Zig!

  • @kalsprite
    @kalsprite Před rokem +3

    this is a great showcase of how painful rust can be. yes, you can make it work, but you're going to burn more calories than you think doing it, and it may end up a rube goldberg device before you're finished.

  • @NoNameAtAll2
    @NoNameAtAll2 Před rokem +10

    would you be interested to look into nushell?

  • @Speykious
    @Speykious Před rokem +47

    Being a Rust programmer, seeing all the blatantly unsafe and unchecked memory operations you can do in C++ is like a nightmare. And here I thought that C only needing one byte to introduce a CVE was bad enough.

    • @Tibor0991
      @Tibor0991 Před rokem

      Trash programmer detected, Rust training wheels deployed.

    • @Tibor0991
      @Tibor0991 Před rokem

      @Watcher *timeout: the monitored command dumped core*

    • @minneelyyyy8923
      @minneelyyyy8923 Před rokem +6

      it's worth noting that this is a manufactured example meant to showcase all of the unsafe stuff you can do.

    • @justanothercomment416
      @justanothercomment416 Před rokem +8

      Eh. That's why standard teachings include not to do most of the things he did. Object/variable life cycle and heap/stack are important concepts. In a couple of seconds of looking at the code I was immediately drawn to the fact he was returning a local. It wasn't even a thought. It's painfully obvious.
      Additionally, there are many of uses of containers which avoid pretty much everything he highlights. This is why STL containers and smart pointers have become so popular. With little effort and a couple set of very basic rules, nothing in his example would have existed. His usage specifically illustrates issues from very poor coding practices is fair. I don't disagree toe shooting is easy. But the implication it's representative of the bulk of the real world usage or that it's difficult to avoid, is not.
      For an example, he's using references where pointers or pointers to pointers make far more sense. Or, perhaps properly design it such that the owning container is passed in rather than created below the stack. As it seems pretty implicit per life cycle, the structure is to persist outside the call stack. Which immediately tells you to create it outside the call stack. So on and so. But again, highlights why even moderately skilled C++ coders don't fall into these traps. Because it's fundamentally how you don't do things.
      Accordingly, it's fair to point out, he went out of his way to create these problems. That should tell you something.

    • @minneelyyyy8923
      @minneelyyyy8923 Před rokem +2

      ​@@justanothercomment416 This whole video was created to illustrate all of the common pitfalls there are in C++ and how Rust makes them impossible. That being said, there is some truth in saying "no good C++ developer would ever write code like this," but they often do. Look at openssl heartbeat and heartbleed exploits.
      Now, one example isn't nearly enough to prove that C or C++ itself is unsafe. It really is OpenSSL's fault, in fact people look down upon it and think that everyone should switch to LibreSSL (also written in C) because of how often they have security blunders.
      I do not think that coming up with specific examples of low level bugs causing exploits is a very good argument. Any good programmer would know that they have to be careful reading user provided data, even if they were not using C or C++. SQL is a good example of this, a high level language, yet one simple mistake can lead to leaked information for millions of users. Python has eval, JS has exec.

  • @rondYT
    @rondYT Před rokem +1

    What is the ‘time’ thing you’re using? Is it a package?

    • @contextfree
      @contextfree  Před rokem

      It's usually available by default on linux. Just say time followed by any command.

  • @arctan2
    @arctan2 Před rokem +1

    Rc right? I'm also writing a terminal ui library in rust so ye 😁😁

  • @Jack-hd3ov
    @Jack-hd3ov Před rokem +2

    Referring to child nodes as kids is hilarious.

  • @michaelmueller9635
    @michaelmueller9635 Před rokem +15

    Rust is C++ with seatbelts and ABS

    • @r2com641
      @r2com641 Před 4 měsíci

      Rust is a car with airbags opening every now and then

  • @m4rt_
    @m4rt_ Před rokem +3

    wtf is that c++ syntax?

  • @dodsjanne
    @dodsjanne Před 10 měsíci +1

    Will D be ignored forever now? :(

  • @johnlimusicofficial220
    @johnlimusicofficial220 Před 11 měsíci +1

    Well I'm not a big expert in C++, what does that arrow syntax in function declarations mean? What does it do and when was it added ?

    • @contextfree
      @contextfree  Před 11 měsíci +3

      That's trailing return type rather than before the function. You only rarely need it for some template situations, but odd folks like me prefer using it consistently. Looks like it was added in C++11.

    • @johnlimusicofficial220
      @johnlimusicofficial220 Před 11 měsíci +1

      @@contextfree aha, got it... Thanks for info :)

  • @yash1152
    @yash1152 Před 3 měsíci +1

    2:13 u forgot -Wextra

  • @thingsiplay
    @thingsiplay Před rokem +2

    You are such a troublemaker!

  • @minneelyyyy8923
    @minneelyyyy8923 Před rokem +9

    I personally feel like memory safety is a bit over stated. Memory bugs are on the same tier as logic bugs to me. I personally prefer using C for a lot of my applications even if it's possible to create memory bugs just because I prefer the language.
    Call me crazy, but I prefer the simplicity C provides. When I read and write code I do not need to think about the code itself, I just need to think about the steps necessary to get a task done. It's a little more inconvenient to have to think about memory, but it doesn't bother me all that much; worrying about memory is much easier than worrying about high level issues.
    It's worth noting that the code I am usually writing is relatively close to the operating system and so it's a lot easier to think about a task in terms of low level steps. I actually do not program all that often, especially not high level tasks. When I do program I like to challenge myself to write it as close to the OS as possible. I write system utilities in C without libc and exclusively using system calls, as opposed to writing them in Rust. As a result, they end up being less than a kilobyte in size each.

    • @pedrogodinho4649
      @pedrogodinho4649 Před rokem +7

      For the record, not using libc would not make your binary any smaller if you're not statically linking (in fact, it will probably make it larger since you're rolling out your own solution instead of using the libc that's likely already in the computer anyway). If using libc is an option at all, I see no reason to avoid it. If you work with embedded devices and such, rust is also a great option there.
      And regarding the simplicity of the language, I'd say that's less a feature of the language and more of a feature of how used to it you are. If anything you can say it is less verbose, so you spend less time actually typing it out, but in any serious project the time you spend actually programming is a minority, you spend the majority of the time reading, debugging, and thinking out what you should do. Absolutely valid to just like C more though, I still love myself some C
      But I really really REALLY cannot say enough that memory safety is NOT overstated. Microsoft reported that 70% (70%!!) of all vulnerabilities they find and address on their products come from memory safety, each year, for the last 12 years! From simple buffer overflows, to use after free errors, to heap corruption, even a single bad memory access is enough to cause arbitrary code execution, even on modern software and OSes, with all the builtin safeties (NX, PIE, canaries, RelRO...)
      And the worst of all is that memory errors often don't cause issues when they happen, but later, making them exceptionally hard to debug (for instance, if you cause a heap corruption, the program might not segfault or even show any abnormal behaviour until one to several allocations later)
      Please, take your memory safety seriously guys

    • @minneelyyyy8923
      @minneelyyyy8923 Před rokem

      ​@@pedrogodinho4649 First of all, I appreciate the long and well thought out reply. The replies I was expecting to receive would've looked a lot ruder.
      In regards to your first claim that using libc would produce a similarly small executable, that would seem logical, however I have never had any success getting a very small binary that is also dynamically linked. Also, it's more fun to write it yourself, so why not?
      Second, I absolutely agree that memory issues can cause significant security issues, as well as other issues such as degraded performance. However, I think a lot of people are exaggerating slightly. Most memory bugs, while sometimes well hidden, do not have any impact on security. I think that overall, the people who claim "nobody should ever use a memory unsafe language ever again" are just taking an extremist approach to the issue. I absolutely do take memory safety seriously, but I can sleep soundly knowing that there might be a memory bug in my code hiding somewhere.
      On the topic of that 70% issue, that figure has a few issues. For one, a lot of other factors could come into play. That figure is based on bugs discovered (obviously, they cannot count bugs they have yet to discover), so it could also be reasonable to come to the conclusion that they are easier to find than regular bugs, correct? Another issue with this figure is that it is assuming something about all code bases based on a very small sample size. Most people do not directly claim that they think that 70% of all C bugs are memory bugs, or that this figure applies to all code bases, but I believe that is sort of what they're subconsciously getting at.
      So in other words, that figure alone does not actually mean much, and can be interpreted in multiple ways.

  • @malwaretestingfan
    @malwaretestingfan Před rokem +3

    I think that the D programming language should have also been showcased. D has bound checking, guaranteed default initializations, protection against dangling pointers by lifetime analysis (DIP25 and DIP1000), a @safe mode (which roughly emulates the guarantees of non-unsafe C# code) and even a prototypal borrow checker (@live functions).

    • @contextfree
      @contextfree  Před rokem

      I knew of some of those things, but this is the best single list I've seen. Thanks!

    • @contextfree
      @contextfree  Před rokem

      Is the safe by default mode for compiling D?

    • @malwaretestingfan
      @malwaretestingfan Před rokem +1

      @@contextfree Not now, but @safe by default is intended to be implemented in the future, according to Atila Neves own statements in "My Vision Of D's Future". Besides, D is still deprecating heaps of unsafe or unnecessary features, while paving the ground for implementing language constructs like native tuples and co-routines, plus features like the aforementioned @live functions have not yet been perfected enough for use in coding, so it's expected that there will be a delay.
      To ensure the project as a whole uses @safe code, you could consider applying the @safe attribute to the main function.

  • @dodsjanne
    @dodsjanne Před rokem +1

    Aaaaaand D

  • @mikesmith9451
    @mikesmith9451 Před rokem +3

    These millions lifetimes for a simple struct (and it still doesn't compile or somehow it does compile but I don't know why) are one of the reasons I gave up rust. Just moved to cpp without c pointers and data-oriented design for better memory safety.

    • @scosminv
      @scosminv Před rokem

      zig is a good option once it becomes more stable.

  • @m4rt_
    @m4rt_ Před rokem

    0:32 don't people call them children, and not kids?

  • @DejaimeNeto
    @DejaimeNeto Před rokem

    yez

  • @Speykious
    @Speykious Před rokem +6

    I think you should've clarified that this example was designed to show all the memory safety problems that can happen in a real-world project and how the different languages tackle these problems. Because of course returning a reference to a local variable in C++ isn't the right thing to do. But that doesn't mean that the C++ example is not relevant, since you tried to do exactly the same thing in all languages to see how they tackle the issue.

  • @n0ame1u1
    @n0ame1u1 Před rokem +6

    Memory safety in C++ 😂

  • @irlshrek
    @irlshrek Před rokem +14

    Rust is so damn cool

  • @imlassuom
    @imlassuom Před rokem +13

    C++ : you are free to break the rules, but only if you know what you are doing!
    Rust: this is my house my rules and nothing else!

    • @gogudelagaze1585
      @gogudelagaze1585 Před rokem +3

      Rust is actually the tiger mom of programming languages. Change my mind.

    • @someonespotatohmm9513
      @someonespotatohmm9513 Před rokem +9

      More like c++: rules, nay they are more like guidelines anyway.
      Rust: you are free to break the rules, but only if you explicitly say you know what you are doing!

    • @Speykious
      @Speykious Před rokem +2

      @@someonespotatohmm9513 yup, that's more accurate

    • @whaisonw2865
      @whaisonw2865 Před rokem

      Now, take my unsafe

  • @leshommesdupilly
    @leshommesdupilly Před rokem

    Personally, I just add this comment at the end of the main function:
    //Let the os free the memory for me

    • @dynfoxx
      @dynfoxx Před rokem +2

      One thing to remember is that just like there is life before main there is life after it. That can be used against you if your not safe.

  • @morglod
    @morglod Před rokem

    no one: ...
    rusters:
    "why when I wrote shit code, compiler did not fix it????"

  • @kamertonaudiophileplayer847

    It looks like the video for Rust programmers.

  • @Tibor0991
    @Tibor0991 Před rokem +8

    To be fair, the C++ code is the equivalent of that "Where does the circle go? In the square hole" meme.
    You purposefully wrote all that C++ code wrong, and it's semantically similar to writing your Rust example only with unsafe blocks and then complaining that Rust sucks because "nobody is stopping you from wrapping the code in unsafe".

    • @JohnWilliams-gy5yc
      @JohnWilliams-gy5yc Před rokem +4

      When people demands implicit bound-checking, it amazes me everytime these days because range-for exists for a decade.
      People blame C lets users run with scissors. C++ evolves so rapidly that you need no scissors in your hands anymore. You just need to write modern codes.

    • @hagbardceline9866
      @hagbardceline9866 Před rokem +3

      I can reject such a PR that you made in my Rust Project without looking at it - i can't with C++

    • @Tibor0991
      @Tibor0991 Před rokem +3

      ​@@hagbardceline9866 the sight of an asterisk or "new" in C++ is already ground for a PR rejection, without looking at it.
      Also, how do you know if unsafe was necessary in the context of the PR or not?

    • @pedrogodinho4649
      @pedrogodinho4649 Před rokem +3

      Not quite... The point isn't to show real code examples, but show situations in which memory errors can occur and how the compiler and tooling for the languages can help you find and fix them. Unsurprisingly, since rust was literally built on the grounds of straight up preventing these errors, rust's compiler errors you out before you can even run the program. As C++ was not designed with this objective, there are obviously situations in which you can slip in memory safety problems into your code. That's what the video wants to show I think

  • @olafbaeyens8955
    @olafbaeyens8955 Před rokem +2

    I build projects in C++ but later jumped to C# because I could develop faster. Compiles faster as your code becomes more and more complex and is more readable.
    Recently I jumped to Rust. Was a big brain challenge without classes, had to rethink my way to develop.
    Beginning this week I was surprised that I could develop equally as fast as C#.
    Later this week I discovered that I can actually develop faster in Rust than I can in C#. I mean complex code.
    C# is still a favorite language but Rust is clearly now my number 1.

  • @mateushesed
    @mateushesed Před 19 dny

    Yeah, Zig was much better than Rust here. And damn, Rust is very unreadable. It manages to be as bad as reading code in C++.

  • @neociber24
    @neociber24 Před rokem +5

    I don't know much about C++, but I always read that modern C++ is safer.
    So it's not true?

    • @SaHaRaSquad
      @SaHaRaSquad Před rokem +24

      Safer than old C++, not as safe as some newer languages.
      Zig and Rust don't need to be backwards compatible with unsafe code, so they were built from the ground up for better static checks.

    • @pedromiguelareias
      @pedromiguelareias Před rokem +19

      Value-based C++ with RAII for resource-managing classes is the key. However, people wish to look smart online and produce dangerous C++.

    • @dj-maxus
      @dj-maxus Před rokem +3

      I heard that full memory and type safety are somehow available in modern C++, but, apparently, they are somewhere deep and have to be set up, idk. Sounds like a complete mess comparing to the Rust way

    • @embeddor3023
      @embeddor3023 Před rokem +1

      @@pedromiguelareias iterator invalidation is still something very common even if you stick to value semantics

    • @lx2222x
      @lx2222x Před rokem

      As long as you have some IQ, stick to certain rules, use smart pointers and don't use any obscure and unsafe C functions you won't create any memory bugs. Don't listen to any retards saying C++ is an unsafe language and that's is why no one should use it bla bla. C++ was not created for retarded people, only for those who know what they do.

  • @ajinkyakamat7053
    @ajinkyakamat7053 Před rokem +12

    This is not the best example for C++. Why would you not return the tree instead of returning a reference. In fact the compiler warned you not to return a reference. You did it anyways by jumping through hoops

    • @contextfree
      @contextfree  Před rokem +14

      Yeah, a move in either C++ or Rust would make the most sense if I want the tree. This is just toy code showing how things can go wrong. Presumably real-world code more often hits bad things from just being more complicated in the first place.

    • @ajinkyakamat7053
      @ajinkyakamat7053 Před rokem +19

      @@contextfree C++17 has guaranteed return value optimization. So in this case you don't have to use std::move to get the best performance. Just returning the tree is infact the fastest, the most obvious and safest way .
      That's why I think this toy example is actually wrong when it comes to showing how things can go wrong. But your point is correct. Yes, returning a reference to a stack object in C++ is bad.
      I would say in C++ two good rules of thumb for such kind of errors is to not optimize prematurely and to not try to outsmart the compiler.

    • @switchblade6226
      @switchblade6226 Před rokem +9

      @@contextfree It also depends on what kind of C++ you are writing. Using modern C++ and taking advantage of the compiler (and not ignoring/silencing errors) is going to give you safe and concise code.
      Using C++98 era style and practices will lead you into a deathtrap.

    • @contextfree
      @contextfree  Před rokem +4

      @@ajinkyakamat7053 Guaranteed RVO is nice for sure. It's still effectively a kind of move but in nicer form. And I think RVO is effectively the same thing as a move in Rust, except Rust describes it more easily in more contexts (including as function arguments) because of the differences in language semantics. Maybe there are differences between RVO and Rust moves that I haven't thought through properly, though. And maybe I should study it better sometime and make a video just about this. Might also be some Nim to bring in.

    • @metal571
      @metal571 Před rokem +1

      You can additionally use at a bare minimum:
      -Werror -Wall -Wextra -pedantic -pedantic-errors
      -Wall is very unfortunately named and includes almost nothing compared to using all of these warnings. On top of that, the clang-tidy static analyzer should help find further relatively obvious memory unsafety issues, but yeah...it's no match for rustc
      Edit: you probably will want to specify the language standard as well with -std=c++17 for example. Unfortunately GCC defaults to using loads of GNU extensions to the standard without -std

  • @LarryGarfieldCrell
    @LarryGarfieldCrell Před rokem +11

    So what I'm hearing is... All languages suck, it's just a matter of how. :-)

    • @contextfree
      @contextfree  Před rokem +5

      Or all languages are awesome! Buy yeah, they have they're own tradeoffs.

  • @theoDSP
    @theoDSP Před rokem +9

    Damn, Adding those boilerplate lifetime tags in Rust is ugly. It's developer ergonomics suck and you can't perform the job in the end. Don't like it at all.

    • @phenanrithe
      @phenanrithe Před rokem +3

      Indeed, it's the part of Rust I'm still less comfortable with, but like all concepts you get used to it and you only have to use them in specific cases. The compiler is getting smarter at helping with the syntax too.

    • @diadetediotedio6918
      @diadetediotedio6918 Před rokem +4

      The lifetimes are for explicit checking and declaring memory management, the objective of Rust is not to hide the low-level magic from developer but to allow him to stay safer and know very well how the final code will run

    • @wrong1029
      @wrong1029 Před rokem +6

      I work with rust professionally and I rarely have to use them.

    • @shu3684
      @shu3684 Před rokem +4

      You rarely have to use them and it's for your own benefit, not really a boilerplate as you might think

    • @itellyouforfree7238
      @itellyouforfree7238 Před rokem +1

      maybe you are just not smart enough

  • @platin2148
    @platin2148 Před rokem

    It’s more a how dumb can you program until you create a potential issue. Security wise this is all a none issue as the program is simply not accessible.

  • @pedromiguelareias
    @pedromiguelareias Před rokem +7

    So... what's the advantage of Rust or Zig with respect to C++ ? Who, in the current economy. is going to invest the many millions that were dropped on C++ libraries, tools, software, gui toolkits and books? Zig looks good, as does Go, but failure is not an option, specially in poor countries.

    • @neociber24
      @neociber24 Před rokem +11

      Well, the advantage is safety, but that it's just one factor. You can't just throw away C++, it's hard to hire in new technolgies

    • @phenanrithe
      @phenanrithe Před rokem +16

      I can't speak for Zig that I don't know, but Rust has already quite a large library though not anywhere comparable in maturity or scope. At this stage, it's certainly a little more adventurous than a well-established language - we see the same about Kotlin vs Java, and there are still endless discussions on many features. But the safety advantages should incite *some* companies to adopt it to avoid spending too much time on fixing bugs after the software is deployed on the field. A recent example of such adoption is the Linux kernel. Even Google is starting to use Rust instead of their Go language.

    • @SaHaRaSquad
      @SaHaRaSquad Před rokem +21

      Microsoft just shipped Rust code in Windows, and many other corporations started using it too. And if companies invested millions in C++ they can do so with new languages as well, I don't see the problem.
      What's the advantage compared to C++? Less bugs, and bugs cost a lot of money.

    • @pedromiguelareias
      @pedromiguelareias Před rokem +3

      @@SaHaRaSquad I don't have a strong opinion. In Microsoft websites, C++ is still the preferred technology, with WinUI3 being available with C# and non-managed C++. It's not clear that Rust, Zig or Go will succeed in displacing huge investments. Too many offerings competing for attention.

    • @MaxiJabase
      @MaxiJabase Před rokem +4

      @@pedromiguelareias I wonder what Google's Carbon role will play in this situation.

  • @maxpayne5056
    @maxpayne5056 Před rokem +5

    Rust is so annoying to watch. I can make the same memory safe code in Nim without any of that hustle.

    • @itellyouforfree7238
      @itellyouforfree7238 Před rokem +3

      yeah sure, and maybe in bash too...

    • @maxpayne5056
      @maxpayne5056 Před rokem

      Sure, sure that's what most rust fans do. Learn a bit of it, then go around and be schmuck towards anyone disagreeing with you.

    • @itellyouforfree7238
      @itellyouforfree7238 Před rokem +2

      @@maxpayne5056 well I surely didn't learn just "a bit of it" LOL. maybe you did. and maybe you didn't even understand what it's all about. you can't even begin to compare nim to rust. i hope you at least see that, otherwise you have no business in programming

    • @maxpayne5056
      @maxpayne5056 Před rokem

      @@itellyouforfree7238 Save it for someone else, you immediately proved my point. If replying to comments is your job, then how much programming you have time to do?

    • @itellyouforfree7238
      @itellyouforfree7238 Před rokem +1

      ​@@maxpayne5056 how the heck did i prove your point by explicitly saying you're downright wrong?! are you on drugs? moreover it's fucking sunday! do you expect me to be working at 5PM on a fucking sunday?!

  •  Před rokem

    Rust made C++ obsolete.

  • @GordonChil
    @GordonChil Před rokem +5

    Using “kids” as an alternative nomenclature for “children” in computer science is stupid. 👎

    • @QingxiangJia
      @QingxiangJia Před rokem +1

      As a non-native speaker, can I ask why?

    • @QingxiangJia
      @QingxiangJia Před rokem

      Oh, I think I know: kids mean human. But children can refer to non-human objects.

    • @contextfree
      @contextfree  Před rokem +16

      Children also usually means human. I prefer "kids" because (1) it's shorter and (2) it has a standard plural. Both things make the code clearer. The only obvious con to me is that it's less common in code.

    • @wrong1029
      @wrong1029 Před rokem +9

      You're just saying that because your last name is child 🤣

    • @contextfree
      @contextfree  Před rokem +2

      @@wrong1029 I'm so slow.

  • @yooyo3d
    @yooyo3d Před rokem +1

    This is just wrong.
    Zig and Rust have awful syntax and trying to solve problems on wrong way.
    Zig looks worse than newest C++ standard.
    Rust have wrong premise that borrowing would solve all problems.
    Please stop with this. This is not programming, this is fighting against compiler.