3 ms·
Isn’t that the same with as C? You can use alloca to allocate on the stack, giving you the same benefit as RAII (I think?). I haven’t used C++ so maybe I’m spea
by mruts 7y ago
Isn’t that the same with as C? You can use alloca to allocate on the stack, giving you the same benefit as RAII (I think?). I haven’t used C++ so maybe I’m speaking out of my ass though.
But regardless, you could use the Boehm GC for objects you don’t feel like tracking the lifetime of.
- monocasa 7y agoRAII can help you protect heap memory (and other resources besides memory) because it's more about registering code to run on the end of object lifetimes rather than just the memory being deallocated. Alloca isnt really a great option for async flows for instance, to say nothing of file descriptors, or abstract concepts like 'the idea of this mutex being locked'. Alloca is also terrible for memory passed on to other threads obviously.. Albeit most of that is moot when compared to a GCed language like the global discussion.
- BubRoss 7y agoStack memory doesn't need to be managed regardless. With boehm gc you are going to run into the same problems of gc. RAII works well for lots of things - heap memory, mutex locks, reference counts, files, io, etc.