Question

I found interesting moment: the atexit() function works differently for bionic and glibc. Here is an example:

#include <cstdlib>
#include <cstdio>
extern "C" {
    void one(){
    printf("one\n");
    }
    void two() {
    printf("two\n");
    atexit(one);
    }
}
int main() {
    atexit(two);
}

Results for bionic:

two

Results for glibc:

two
one

Why do the results differ?

Was it helpful?

Solution

It is unspecified whether a call to the atexit function that does not happen before the exit function is called will succeed.

ISO C standard, §7.22.4.2. So both behaviors are compliant; you can't reliably register a function using atexit while exit is already running its atexit handlers.

OTHER TIPS

This behavior is unspecified. You can define multiple functions to be called using atexit() multiple times, but you must not use it once you're already exiting the program (i.e. once you've left main()).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top