3 ms·
In C++, allocation errors don't need to throw an exception. Object* obj = new (nothrow) Object; If the above allocation fails, obj will have the value NULL.
by scott_s 15y ago
In C++, allocation errors don't need to throw an exception.
Object* obj = new (nothrow) Object;
If the above allocation fails, obj will have the value NULL. It's tedious, but you could replace the default, throwing allocation with a non-throwing allocation in all places of the STL. However, various parts of the STL throw other kinds of exceptions. But, I think one could have a compromise, where you deal with exceptions from libraries you use, but you don't throw any yourself, instead using C-style error handling.
- cageface 15y agoIf you replace the STL allocator with a non-throwing allocator how do you know that that vector you just push_back'd didn't actually allocate any new memory?
- scott_s 15y agoYou don't. It will segfault.
- james4k 15y agoIndeed, you have to roll your own data structures. This is pretty common in the game development world, where exceptions are considered to have unpredictable performance characteristics.
- pestaa 15y agoThat is a pretty tight requirement, considering the internals of Ogre 3D engine. It is full with exception classes (some might argue it is way too overengineered.) Are custom data structures available as open source that don't use exceptions? Or perhaps can you please name an engine that already has this?
- james4k 15y agoThere is EASTL, though I've never used it myself. https://github.com/paulhodge/EASTL https://github.com/paulhodge/EASTL Valve's Source SDK makes extensive use of their own data structures, however it is very specialized. For example, linked lists are allocated as growing, contiguous blocks of memory to reduce cache misses. The Doom3 source code might be worth looking at, but I'm sure it's the same story. https://github.com/TTimo/doom3.gpl https://github.com/TTimo/doom3.gpl At the very least, there's a lot of great reference out there.