r/cpp • • Jan 11 '26

CppCon Making C++ Safe, Healthy, and Efficient - CppCon 2025

https://youtu.be/p52mNWsh-qs?si=Bz6pyW6d0SMLCZxV

Now with some updated content since the ACCU talk, and the Q&A is nonetheless interesting.

55 Upvotes

103 comments sorted by

View all comments

Show parent comment

-1

u/KFUP Jan 11 '26 edited Jan 11 '26

to be so superbly expensive to turn on vs something like Rust

Not really sure what are you talking about here. He's talking about correctness, which contracts can help with, and not something rust or borrow checkers can help with.

His main talking point is that correctness should be considered a memory safety issue, which rust doesn't help with and ignores as not one. Worse was with memory leaks, as it was once considered a memory issue in rust in the early days, then magically stopped being a memory issue once they realized that the borrow checkers can't solve it, basically redefining memory issue as anything the borrow checker can solve, anything it can't is not is not a memory issue by definition, even memory leaks.

And no, "crashing is safe" is a terrible argument, try using that in a critical airplane software, and the plane will have a very memory safe crash with the ground once it runs out of memory because of a memory leak, not counting memory leaks as memory safety is insane.

8

u/James20k P2005R0 Jan 11 '26 edited Jan 11 '26

https://youtu.be/p52mNWsh-qs?si=uKVA1QPwJ8h7c7Z8&t=2607

This is the segment of the talk I'm referencing. This is about checking all UB in C++ under contracts

There are also explicit references in there to enforcing exclusivity via runtime checks, ie implementing a borrowchecker that operates purely at runtime. On top of that, you need to check all memory accesses, watch for thread safety issues, guard against stray writes etc. This all has a very high runtime cost, as you're essentially deploying a much more comprehensive ub/asan which is already superbly expensive to run

Adding a runtime to C++ like this is purely about memory safety, not correctness

6

u/maxus8 Jan 12 '26

> Worse was with memory leaks, as it was once considered a memory issue in rust in the early days, then magically stopped being a memory issue

The rust compiler doesn't depend on the code not to leak memory so it's not UB for it to happen, and memory safety is mostly about preventing UB. The issue was that functions for leaking memory were still marked as unsafe and there was unsafe code in the wild (and in std) that depended on leaks not to happen for them not to cause UB.

If you want to have a consistent model of what is safe and what is not, you can't just tell everyone... not to do that kind of stuff. You need to decide that either:

- methods for creating ref-counted pointers is unsafe (at least in some cases, some of them useful) AND disallow catching panics, because RC cycles and panics during memory freeing may cause leaks

- you cannot make safety of your code dependent on memory not leaking in arbitrary code. This also let's you mark methods that leak memory as safe.

The decision was to go with the second option. The fact that you can't guarantee lack of leaks makes memory safety weaker, but it still gives you lack of UB in safe code.

5

u/QuaternionsRoll Jan 12 '26

basically redefining memory issue as anything the borrow checker can solve, anything it can't is not is not a memory issue by definition, even memory leaks.

lol

For the purposes of this work, we define a software entity (a module, thread, or complete program) to be memory-safe if (a) it never references a memory location outside the address space allocated by or for that entity, and (b) it never executes instructions outside the code area created by the compiler and linker within that address space.

Dinakar Dhurjati, Sumant Kowshik, Vikram Adve, and Chris Lattner. 2003. Memory safety without runtime checks or garbage collection. SIGPLAN Not. 38, 7 (July 2003), 69–80. https://doi.org/10.1145/780731.780743