In C++ you may use RAII
]. Do initialization in the constructor, do some cleanup in the destructor. Take special care of exceptions in the destructor.
In C you must tell the client of your code to follow the protocol: call first some invented
function, then at the end some invented
function. Both functions are part of your library and do what you require in your question.