Live data from Hacker News

Memory safety is necessary, not sufficient

steveklabnik.com

161–162 of 162 posts

Re: Memory safety is necessary, not sufficient

#161
post #157

Earlier quoted context omitted.

This is simply not true. Almost all optimisations rely on undefined behavior, see https://news.ycombinator.com/item?id=38760475 Note that I am not talking about UB like signed integer overflow. Removing that would slow down programs by a couple of percent. The important type of UB is pointer provenance. This ensures that e.g., writing to a random memory address is UB.

Almost all optimisations in C/C++ compilers depend on undefined behaviour, because practically no behaviour is defined! The trick is to define behaviour, which is what other programming languages do. E.g.: in both C# and Rust, integers have fixed sizes. A C# Int32 is equivalent to a Rust i32. Only God knows what a C/C++ "int" is. It could have 17 bits and use ternary.

As I said, this isn't about ints, it's about pointers. What behavior will you define in C for writing to a random memory address?

Re: Memory safety is necessary, not sufficient

#162

Earlier quoted context omitted.

They affect performance, forcing the dev to insure they aren't doing unsafe/ill-defined things which is almost impossible on huge code bases in c/c++

They are unhappy being forced to write code that is free of undefined behavior? It reminds me of the joke: Alice claims she is very fast at mental math. Bob asks "what's 4821 times 5997?" Alice replies "ten thousand." Bob says, "What, no, that's wrong, very wrong." Alice says, "But it was fast!" Are you telling me C / C++ developers are like Alice? When given the choice between fast undefined behavior and slower but…

You seem to be assuming that on a large code base that you have top 1% programmers. I suppose that is the case for some places that are paying $500k average pay, but most businesses don't pay that and have average developers and a few top 5% if big enough. That's why tools that check for issues like linters and sanitizers help, especially if you make them part of "the process" for checkin
Post reply on HN