5 ms·
I don't really understand why RAII is related to many small memory allocations. Is memory leaking less of a problem if it is in just a few big allocations? The
by giomasce 5y ago
I don't really understand why RAII is related to many small memory allocations. Is memory leaking less of a problem if it is in just a few big allocations? The proposed solution of using handles instead of pointers is a tradeoff that can make a lot of sense in some situations (it basically trades slower access to objects for better safety and defensive programming), but it is rather independent of RAII. Handles or not radically different from pointers: they still need an allocator (and you can say that your custom one is better tailored to your problem, but writing an allocator is not easy, and it's all to be seen if yours is better than the standard ones) and you still need to remember to invalidate handles, which is what RAII is really for.
So using handles can be a good idea, both in C and in C++, but it's really orthogonal to RAII. It can partially compoensate the lack of copy constructors and destructors, but if you need to keep a reference count to a handle you're still short of a good solution, and your code must be ready at any place to cope with a expired handle.