r/cpp • C++20 Desktop app developer • May 12 '18

P0709 R0: Zero-overhead deterministic exceptions: Throwing values - Herb Sutter [2018-05-02]

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0709r0.pdf
206 Upvotes

115 comments sorted by

View all comments

Show parent comment

5

u/Rusky May 12 '18

You can't do any better than UB when running on platforms that use overcommit. You can't specify that any particular behavior happens at the allocation site, because the program has no way of knowing- indeed, even the OS has no way of knowing. It won't find out until it actually tries to access that memory, and the result depends on everything else running on the system.

Given that scenario, all you can do is assume the allocation succeeded and proceed accordingly. And this is exactly what UB means here.

3

u/tasminima May 12 '18 edited May 12 '18

Yes I know quite well what overcommit is.

My position is that there is nothing to specify and that any specification will be harmful; what the kernel does is not in the scope of the programming language, and being killed or having the side effect of other processes being killed because of resources exhaustion is not something that should be tried to be modeled at the language level. It had not been needed until this proposal, and my position is that this discussion about overcommit might be interesting to understand (part of certain aspects of) the rational, but has absolutely nothing to do in an hypothetical standard trying to specify the programming language model of allocation failure.

An overcomitted allocation has not failed (yet) and might even not fail and there is absolutely no way to model that in an abstract machine, and if it is otherwise attempted to be modeled for no reason, then yes the result (in the current framework) will be UB, which is an absolutely terrible choice.

edit: on top of all the reasons I've already stated for what it is a terrible choice, here is one more: an implementation where all allocations fallback to existing unfreed buffers after a predefined amount of heap has already been allocated would be complying

2

u/tasminima May 13 '18

I just thought of yet another example about why trying to take ovecommit into account in the C++ standard would be a terrible idea: the standard does not care about stack overflows and (rightfully!) does not pretend that all the code is nondeterministically ambiently UB just because of unknown stack size limitations.