7

CRT function atexit() could register a function to run after main function returns. I am wondering what's the typical scenario to use this? Is it (atexit) really necessary?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Thomson
  • 20,586
  • 28
  • 90
  • 134
  • To free up any global statics (which could be set up before `main` or on a just-in-time basis) is one use. – Bathsheba Aug 04 '14 at 09:36
  • For completness: "*... could register a function ...*" also **multiple** functions may be registered. – alk Aug 04 '14 at 09:39
  • Note that `atexit()` is a standard C function and is mandated by each of the C standards (C90, C99, C11, C18, C23, …). It is not specific to the Microsoft runtime environment. – Jonathan Leffler Apr 10 '23 at 06:50

5 Answers5

6

I guess its primary use is when you don't have control over main and you want to make sure something is called at the end of it.

It's sometimes used by libraries that don't want to insist that the user program calls their cleanup functions explicitly before terminating the program.

It's also used in the phoenix singleton pattern (see Modern C++ Design by Andrei Alexandrescu).

Stuart Golodetz
  • 20,238
  • 4
  • 51
  • 80
6

It can be used for what you want that need to be executed EVERYTIME an application is shutting down. By using that, you don't need to bloat your code by adding all the cleanup code before each exit() you can find in your code.

Some use cases :

  • Clean a temporary folder
  • Print a memory dump
tomaoq
  • 3,028
  • 2
  • 18
  • 25
  • atexit is not only purposed to run on application shutting down, it is binary/image based, and can be also called when unloading the image. – Thomson Aug 14 '14 at 10:04
3

One of the main uses of atexit is for libraries to perform cleanup on program exit. Note that atexit is called when exit is called, not when the program aborts or crashes, so you can't do cleanup on assertion failures and so on. It is also not called if the program calls exec.

You can call it directly in the main program if you want, if you have a library that might call exit for some reason.

Note that you can only register a limited number of atexit handlers, where 'limited' depends on your operating system, and so it returns an error status.

It gives C programs a similar ability to calling the destructor of a static variable in C++.

I've used it to delete temporary files, or (once or twice) to reset some hardware registers. Generally it's not necessary to use it to close files or rlease memory, because the O/S will do that for you.

Tom Tanner
  • 9,244
  • 3
  • 33
  • 61
1

When writing libraries ... Imagine a library that on crashes save the stack on a pre-defined path (or mails the trace).


EDIT - as mentioned on the comment, this answer is wrong. Don't read it. Too late.

elcuco
  • 8,948
  • 9
  • 47
  • 69
-1

Exceptions can be handled in atexit ().Assume multi process environment. There is one hardware resource is physically available. Any one of the process can use that h/w at a time. Now process1 is acquired the h/w resource and after processing process1 not released h/w resource. To release the h/w resource this atexit() may be used, so that process2 can acquire h/w effectively.

mahendiran.b
  • 1,315
  • 7
  • 13