title: How to use atexit in C++
body: <p>I am working in a C++ homework and the instructions say that it would be good to use atexit() as follows:</p>
  
  <blockquote>
    <p><em>"Make sure that the program exits cleanly if aborted (Using an atexit() handler is a good way to do this)."</em></p>
  </blockquote>
  
  <p>The homework has nothing to do with aborting program execution (it is about memory management), it is only a suggestion but I am not sure how to use it. The reason for handling this situation is because the program might take too long to execute depending on certain parameters when it is run. If it takes too long I may type CTRL-C or another combination of keys to stop execution abnormally.</p>
  
  <p>This is what I have done. </p>
  
  <pre><code>void AtExitFunc()
  {
      release_allocator();
  }
  
  int main(int argc, char ** argv) {
      // Set default values.
      int intMemSize = 512;
      int intBasicBlock = 128;
  
      atexit(AtExitFunc);
      init_allocator(intBasicBlock, intMemSize);
      ackerman_main();
      release_allocator();
  
      return 0;
  }
  </code></pre>