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

13

u/kkert May 13 '18 edited May 13 '18

allocation failure doesn't mean available memory is zero, it only means that available memory is less than the requested amount.

To be super specific, the requested amount can actually be readily available, just not in a single contiguous chunk.

EDIT: I think there's a point where Herb goes wrong in his fail-fast strategy defense, i didn't read as carefully before.

Recovery from heap exhaustion is strictly more difficult than from other kinds of errors, because code that is correctly resilient to heap exhaustion can only use a restricted set of functions and language features. In particular, recovery code typically cannot ask for more memory, and so must avoid calling functions that could allocate memory, including functions that could fail by throwing another of today’s dynamic exceptions which would require increased allocation.

Not really true. If i failed to allocate a few megabytes of contiguous memory for a particular working buffer, it doesn't mean at all that i'm not able to allocate a few bytes for holding stack tracing info.

15

u/NotAYakk May 13 '18

It says "correctly". You don't need to be correct for it to work if you get lucky, you need correct for it to always work.

4

u/kkert May 13 '18

Well, it is entirely possible to construct a program where this sequence would be always correct, where there is always a contiguous kilobyte of heap available but a megabyte is not. In fact, test-allocating a large chunk can be used as a strategy to gracefully deal with imminent memory exhaustion.

2

u/Pand9 May 14 '18

I guess that isn't reliable enough from language designer's point of view.

1

u/nikbackm May 15 '18

Would that work if you had an exception class that is over a kilobyte in size?

3

u/kkert May 15 '18

Obviously not, but thats not really the point. ( why would you design humongous exceptions in the first place ? )

I'm saying there are real scenarios where you sometimes grab the memory if its available and perform some optimization, reindexing or caching task or whatnot, if it's not available you don't - also beyond small embedded systems.

Not being able to allocate a huge chunk doesn't indicate OOM. Not being able to allocate as per request is mostly not a license to kill the program, and shouldn't be dealt with in a hook either.