문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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()).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top