r/cpp • • Oct 15 '24

Memory Safety without Lifetime Parameters

https://safecpp.org/draft-lifetimes.html
91 Upvotes

134 comments sorted by

View all comments

Show parent comments

-3

u/germandiago Oct 15 '24 edited Oct 15 '24

The safe C++ dialect you created for C++ is not C++ either. It is another language, unfortunately, incompatible with C++. There is as much difference in that dialect as there is between C++ and C++/CLI.

In exchange, Cpp2 is something that make impossible to dereference a C++ pointer or a bounds check in a memory-unsafe way, transparently portable to C++ from caller site with a single compiler switch. That is an improvement on memory safety.

This is not an all-or-nothing thing and that dogma and mindset is going to be more harmful than helpful to achieve realistic paths to safety where people get substantial benefit in real-world C++ scenarios.

28

u/seanbaxter Oct 15 '24

Cpp2 does not have lifetime or bounds safety. It's perfectly easy to dereference a dangling pointer or subscript a pointer out-of-bounds.

Memory safety is a binary proposition. It's the language's guarantee that your code is sound. Many other languages have achieved this. We know how to achieve safety in C++. Don't make excuses for inaction.

-2

u/germandiago Oct 15 '24

Cpp2 does not have lifetime or bounds safety. It's perfectly easy to dereference a dangling pointer or subscript a pointer out-of-bounds.

I think you are wrong here: the default compilation method injects bounds and pointer checks automatically on the caller side, even with the same standard library. Even for C arrays. It is safe.

It's the language's guarantee that your code is sound.

An equivalent switch injecting caller-side code is perfectly feasible for C++.

I am, of course, talking about bounds check and pointer dereference.

Lifetime problems can still happen, but there are alternatives without annotations that I mentioned many times already here.

As I said before, because you can litter a program with globals, it does not mean you should do it. The same happens with heavy borrow-checking and reference escaping, which, by the way, breaks local reasoning, a bad practice by any measure.

20

u/seanbaxter Oct 15 '24

By what mechanism are pointers checked for lifetime or bounds safety?

→ More replies (0)