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.

56 Upvotes

103 comments sorted by

View all comments

Show parent comment

21

u/SophisticatedAdults Jan 11 '26

It's a bit more complicated than that. Yes, bounds checking for e.g. arrays is cheap. Fully featured checks that aim to prevent any access to wrong memory (e.g. use after free) would be much more expensive.

IIRC Google's work only applies to spatial bound checks, not temporal ones. If you want full temporal bound checks you're probably going to reinvent something like Fil-C and it's going to get much more complicated (and expensive).

1

u/trailingunderscore_ Jan 11 '26

would be much more expensive.

How so?

6

u/SophisticatedAdults Jan 11 '26

Because this requires some sort of runtime (or error correction/parity/pointer tagging scheme) to track whether the lifetime of an object in memory is valid or not.

Bounds checking for arrays is easy, just check if the index is within bounds.

-2

u/[deleted] Jan 11 '26

Bounds checking for arrays is easy, just check if the index is within bounds.

that only requires an extra couple bytes of overhead so that the program can know the bounds. not a big deal.

some sort of runtime (or error correction/parity/pointer tagging scheme) to track whether the lifetime of an object in memory is valid or not.

that would require a single extra byte set during construction/move/dtor so the program can know if the pointer is valid. Totally undoable.

9

u/ts826848 Jan 11 '26 edited Jan 11 '26

that would require a single extra byte set during construction/move/dtor so the program can know if the pointer is valid.

I'm not sure a single byte would be sufficient in general (or at least without that "single byte" being present so frequently as to make for quite a bit of overhead). For instance, consider pointers/views into subspans/subobjects (e.g., std::span, std::string_view, references to elements in a container, pointers to subobjects, etc.). Those don't necessarily know where the boundaries of the object they're pointing to are, so they can't easily check your proposed liveness byte. If you want to use liveness bits/bytes I think you'll need much finer granularity a la ASan shadow memory, and that is quite a bit more expensive.

-1

u/[deleted] Jan 11 '26

so you're suggesting that because it doesn't solve all problems that it shouldn't be used to solve any problems?

I mean just go use rust if you want to use rust. why bother mucking up a perfectly good C++?

If you deference a pointer in rust that's unsafe anyway so why should it be any different in c++? Why would you need to dereference any pointers if you can just copy them and know if they're alive or not? the point of references in C++ is they are always valid (unless they aren't and its not enforced) so if you can just test any pointer for validness and always know for 100% certainty whether its valid, references become pointless.

8

u/ts826848 Jan 12 '26

so you're suggesting that because it doesn't solve all problems that it shouldn't be used to solve any problems?

Talking about tradeoffs of different approaches is fine if you're open about that being your approach. Chiming in to a subthread about the expense of "[f]ully featured checks that aim to prevent any access to wrong memory (e.g. use after free)" with an ostensibly inexpensive approach and saying nothing else would tend to imply that that alternative approach is comparable in terms of capabilities. If not, that probably would have been a nice thing to mention.

Furthermore, a solution that addresses a subset of a larger problem space needs to have some kind of consistent rules/approach/algorithm/etc. for how it works, but it's not clear to me what those would be for your approach. For instance, consider the following function:

void set_int(int& i) {
    i = 42;
}

How would you propose this be checked with your "single extra byte", especially if/when the function isn't inlined? If this solves some problems as you imply, which ones does it solve/doesn't solve and how does the compiler/runtime know how to differentiate between the cases it does solve and the ones it doesn't?

If you deference a pointer in rust that's unsafe anyway so why should it be any different in c++?

Obviously it doesn't need to be, but the entire reason these kinds of conversations are taking place is because Rust provides safe and generally-usable alternatives while C++ doesn't and people are interested in adding similar/comparable capabilities for C++.

the point of references in C++ is they are always valid (unless they aren't and its not enforced)

Well yes, that's the rub, isn't it? In this context of this subthread (i.e., discussing the expense of temporal memory safety), references and pointers suffer from the same temporal issues.

so if you can just test any pointer for validness and always know for 100% certainty whether its valid, references become pointless.

I disagree. Even if you ignore validity issues, references and pointers still have different properties/capabilities (e.g., no pointer arithmetic on references, nullptr is a valid value for pointers, etc.), so if you want reference semantics without the other capabilities that pointers offer then references will still have a use.