2 ms·
No RAII means that allowing early returns in C can increase the likelihood of not performing cleanup along error paths. Consider writing a function called conn
by acuozzo 3y ago
No RAII means that allowing early returns in C can increase the likelihood of not performing cleanup along error paths.
Consider writing a function called connect() which opens a socket and, if successful, calls setsockopt, bind, and friends.
If e.g. bind fails and you choose to return early, then you also have to remember to close the opened socket.
In my own code I prefer to use early returns for all situations except ones like the aforementioned. For those I use a forward goto to a label named "off_ramp" which does the necessary cleanup and returns an error code.