this post was submitted on 28 Nov 2021
11 points (100.0% liked)
Rust Programming
8306 readers
12 users here now
founded 5 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
Can you elaborate this? Is it like when your program wants an integer between 1 and 5 and it gets -420?
I've just finished watching some great talks on undefined behaviour in C/Cpp:
And Rust's own list of undefined behaviour is here, almost entirely do to with humans using
unsafe
but not taking the necessary care with it: https://doc.rust-lang.org/reference/behavior-considered-undefined.htmlThis is a matter of correctness: does your program behave as expected? (Rust can help with this with eg. types to prevent integer overflow)
What we mean with undefined behavior is it's actually undefined. There's quite a lot of constructs you can use in C which will get compiled to different instructions by the compiler and behave in unexpected ways. Or even when compilers agree on a certain way to do it, use-after-free and other patterns can lead your program to taking unpredictable turns.
So, just because your program doesn't have undefined behavior doesn't mean it's correct. But if it's correct, it can't have any undefined behavior. If you've ever noticed how most programs written in C/C++ seem to have hard-to-reproduce bugs (eg. desktop environments) you're very likely to have encountered undefined behavior in the wild.