5 ms·
Some C compiler have a language extension to help with cleanup to help with memory and resource leaks. It lets you do RAII-type resource management that is popu
by criddell 4y ago
Some C compiler have a language extension to help with cleanup to help with memory and resource leaks. It lets you do RAII-type resource management that is popular in languages like C++ but the C version is way uglier.
https://en.wikipedia.org/wiki/Resource_acquisition_is_initialization#Compiler_%22cleanup%22_extensions https://en.wikipedia.org/wiki/Resource_acquisition_is_initia...
- cyber_kinetist 4y agoRAII solves only a fraction of the various scenarios you encounter with memory/resource management. It helps when you're temporarily creating and then destroying a resource right after, but these only hold for very simple use cases and doesn't help with much more complex real-world scenarios. (Example: you'll understand this when you have experience with any graphics APIs, RAII wrappers don't really help you that much and will actually make your life way harder...) My solution for general resource management, is to manage your resources manually on a central store (as you should be), get references to these objects using either pointers or indices, and use techniques like generational references to catch use-after-free scenarios at runtime. Makes things way simpler and performant.
- lenkite 4y agoThis is basically memory arenas right ? How does one handle problems as described here https://www.celonis.com/blog/c-memory-arenas-and-their-implications/ https://www.celonis.com/blog/c-memory-arenas-and-their-impli..., where memory appears to not be returned to the OS ?
- Thorrez 4y agoIn that blog, the arena is managed by the underlying library, and the programmer has no control over when memory is returned, and sometimes the underlying library keeps the memory around when the programmer doesn't want it to. It sounds like cyber_kinetist is talking about manually managing the arena, so then the programmer is in direct control of when the memory is returned, so the problem won't happen.