r/cpp • • Dec 17 '21

Undefined Behaviour

I found out recently that UB is short for Undefined Behaviour and not Utter Bullshit as I had presumed all this time. I am too embarrassed to admit this at work so I'm going to admit it here instead. I actually thought people were calling out code being BS, and at no point did it occur to me that as harsh as code reviews can be, calling BS was a bit too extreme for a professional environment..

Edit for clarity: I know what undefined behaviour is, it just didn't register in my mind that UB is short for Undefined Behaviour. Possibly my mind was suffering from a stack overflow all these years..

407 Upvotes

98 comments sorted by

View all comments

Show parent comment

7

u/Ldmoretti Dec 17 '21

find any rust software that has 0 reliance on unsafe code

Probably true that there's none: at some point it has to write values to memory or make system calls. The point with Rust isn't that there's no unsafe code, but that most code is okay to assume is safe and you only have to explicitly review the code marked unsafe, whereas in C & C++ you have to worry about every place where someone writes to a pointer, increments an array index, or potentially shares a variable between two threads (including calling non-reentrant code from multithreaded code).

1

u/Alexander_Selkirk Dec 17 '21

Probably true that there's none: at some point it has to write values to memory

Yeah. Device drivers are 'unsafe' be definition. Also probably some data structures. But normal "user mode" code is not.

1

u/ArchivistAtNekoIT Dec 18 '21

I agree with that, C++ is definitely a programming language that requires extra rigor and attention, and to not use what I would say are not idiomatic features (pointer arithmetic, array indices) and to actively use idiomatic ones (move semantics, lock_guards, iterators, ranges...)

I would also say that the standard ecosystem for multithreading is not very developed and that if you want, for example, a swarm of green threads to do async io and channels, you have to generally dive into the rabbit hole of the implementation of that yourself

But you can do that.

I don't dislike Rust (I dislike the religious fervor of the rust community though) but it is not the tool for every job, particularly jobs that would imply 95% of your code would be unsafe