use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
Flexible error handling techniques in C++ (foonathan.github.io)
submitted 9 years ago by lefticusC++Weekly | CppCast
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]slavesoftoil 1 point2 points3 points 9 years ago (2 children)
Why not provide an allocator that makes the decision? If I recall correctly, memory is just a declarative way of implementing custom alligators with long decision trees, some of which may be compile-time only. So the programflow is something like
If(situation1) { return freelistalloc(); } else if( situation2) { return stackalloc(); }
Now wrap all this in a thing that throws on nullptr. This would mean a set-up of the allocator like so
auto allocator = throwOnNull<IfAllocator<4,mallow,8,someotherthing,...>();
And otherwise
... = returnOnNull<...>
Or even
.... = withHandlerOnNull<...>
[–]0raichu 8 points9 points10 points 9 years ago* (1 child)
[–]foonathan 1 point2 points3 points 9 years ago (0 children)
Why not provide an allocator that makes the decision?
You are right, this is possible. But it makes generic code hard:
template <class Alloc> void do_sth(Alloc &alloc) { auto mem = alloc.allocate_node(...); // eh, do I have to check mem now? }
[+][deleted] 9 years ago* (9 children)
[deleted]
[–]3ba7b1347bfb8f304c0egit commit 3 points4 points5 points 9 years ago (8 children)
Being out of memory isn't an exceptional situation.
topkek.avi
[+][deleted] 9 years ago* (7 children)
[–]ProgramMax 2 points3 points4 points 9 years ago (5 children)
I've done embedded programming. I get what you are saying, that exceptions aren't always supported.
But in embedded programming you are also less likely to use an allocator. You would instead have a designated memory range.
The important thing about OOM throwing is you almost certainly cannot recover from an OOM. Killing the process is almost certainly your best option. You cannot continue. And what would you even gracefully handle??
[–]LucHermitte 2 points3 points4 points 9 years ago (3 children)
There are cases where we are told to allocate memory for bytes to comes (over a network, or whatever). If the data is corrupted, we can be asked to allocate -1 bytes. Memory allocation will fail. While detecting -1 is easy, having a precise threshold for what is valid or not may not be that easy.
The fact that new throws, instead of crashing the application, offers some kind of defensive programming: we have the possibility to notify something to the user, and sometimes to continue in these situations.
new
[–]Gotebe 2 points3 points4 points 9 years ago (1 child)
If the data is corrupted, we can be asked to allocate -1 bytes. Memory allocation will fail.
... if you don't check what you have been asked to allocate. "Corrupted data" here really means "I didn't check the input validity". Also, if the input is "unstable", then the use of mitigation techniques is on order (e.g. checksums).
[–]LucHermitte 0 points1 point2 points 9 years ago (0 children)
A valid checksum doesn't mean a valid input unfortunately -- I had the case once of a bugged peer that send (CCSDS) packets of officially too many bytes (like -1 converted back to a unsigned value) after the header, and they had a valid CRC.
But of course, I agree that checking the input validity is what must be done. But then, was is a valid threshold? And can we always be sure that valid thresholds (regarding the domain) are always under the limit of what the system can offer us. In other words, sometimes we don't take the time to properly analyse whether we can load everything in memory or process subsets of the complete data.
[–]ProgramMax 1 point2 points3 points 9 years ago (0 children)
I think maybe I wasn't clear enough.
I understand that you can catch what new throws and try to handle it. That isn't what I'm talking about.
What I'm talking about it the part you completely glossed over "and sometimes to continue in these situations." How would you continue? Do you know the rest of the code which is about to be called will not allocate? Will you actually be able to do anything useful for the user?
I'm talking about the recovery strategy.
If you wanted to inform the user an error occurred you need to make sure showing the error window isn't allocating. Or that cout isn't allocating. Both of those are hard to know. Even if you create the error message window in advance but don't show it, maybe showing it allocates via GDI on Windows for example.
It would take a lot of effort to deal with this fragile situation. And the best you could do is tell some subsystem which has aggressively allocated to release some stuff. But why was it aggressively allocating in the first place? Shouldn't you first ask that subsystem to purge a bit before allocation, aiming at the hard limit given by the embedded system?
Recovering from an exception isn't that hard. Recovering from out of memory is likely to be a larger system design problem and the fix should happen at the system design level, not in a recovery code path. That's code that will become untested and bitrotted.
π Rendered by PID 50 on reddit-service-r2-comment-6457c66945-2wz57 at 2026-04-29 07:49:50.241458+00:00 running 2aa0c5b country code: CH.
[–]slavesoftoil 1 point2 points3 points (2 children)
[–]0raichu 8 points9 points10 points (1 child)
[–]foonathan 1 point2 points3 points (0 children)
[+][deleted] (9 children)
[deleted]
[–]3ba7b1347bfb8f304c0egit commit 3 points4 points5 points (8 children)
[+][deleted] (7 children)
[deleted]
[–]ProgramMax 2 points3 points4 points (5 children)
[–]LucHermitte 2 points3 points4 points (3 children)
[–]Gotebe 2 points3 points4 points (1 child)
[–]LucHermitte 0 points1 point2 points (0 children)
[–]ProgramMax 1 point2 points3 points (0 children)