3 ms·
I'd like to know what restrictions cause you not to be able to use RAII. Even if that's the only C++ feature that a project were to use, I'd still consider it
by mihai_ionic 13y ago
I'd like to know what restrictions cause you not to be able to use RAII.
Even if that's the only C++ feature that a project were to use, I'd still consider it a net gain. Anything that makes your program less susceptible to manual resource management bugs in "goto cleanup"-style C is worth using. This doesn't just include memory leaks, but more insidious problems like unclear ownership of various system resources (sockets, file handles...). Things not even a GC would help you with in a long-running server process.
- malkia 13y agoYou can't use longjmp/setjmp - http://en.cppreference.com/w/cpp/utility/program/longjmp http://en.cppreference.com/w/cpp/utility/program/longjmp There are several popular "C" libraries that use them - jpeglib comes to mind, but there are others. Also certain language's runtime environment uses it. There is a way to handle it, but you need to take care.
- mihai_ionic 13y agoEven in C, setjmp/longjmp leave all non-volatile automatic storage duration variables on the current stack frame in an indeterminate state if they are modified between the two calls. To get defined behaviour with regards to destructors, you simply have to make sure that the function calling setjmp doesn't internally use RAII. Call a wrapper function to do the setjmp and you're safe. Of course it's a whole other question whether using setjmp/longjmp to implement "exception handling in C" is a good idea in the first place.