Cè¨èªã®ããã°ã©ã çµäºæã«å¼ã°ããé¢æ°
atexitã使ãã¨ããã°ã©ã çµäºæã«å¼ã°ããé¢æ°ãç»é²ã§ãããï¼void (*)()ã®é¢æ°)
atexitã¯è¤æ°ç»é²ï¼ã¹ã¿ãã¯ã¨ãã¦ç©ã¾ããï¼ï¼ã§ããããã§ãå¾å
¥ãå
åºãã§å®è¡ãããã
ã¾ããçµäºæã®é¢æ°ã¯å°ãªãã¨ãATEXIT_MAXåï¼32åï¼ï¼ç»é²ã§ãããããã
使ãæ¹
#include <stdio.h> #include <stdlib.h> void func1(); void func2(); void func3(); int main() { atexit(func1); atexit(func2); atexit(func3); printf( "Enterãã¼ãæ¼ãã¨ããã°ã©ã ãçµäºãã¾ã\n" ); getchar(); return 0; } void func1() { printf( "çµäºä¸...func1\n" ); } void func2() { printf( "çµäºä¸...func2\n" ); } void func3() { printf( "çµäºä¸...func3\n" ); }
å®è¡çµæ
$ ./atexit
Enterãã¼ãæ¼ãã¨ããã°ã©ã ãçµäºãã¾ã
[Enterå ¥å]
çµäºä¸...func3
çµäºä¸...func2
çµäºä¸...func1
å®é¨
exit, assert, abortããã¦ã¿ã
Cygwinç°å¢ã§å®é¨ããã¨ãããexité¢æ°ã§ã¯å¼ã°ããããabortãassertã§ç°å¸¸çµäºãããå ´åã«ã¯å¼ã°ããªãã
ã¾ããkillã§ããã»ã¹ãå¼·å¶ãããå ´åã«ãå¼ã°ãã¾ããã
#include <stdio.h> #include <stdlib.h> void func1(); int main() { atexit(func1); abort(); /* exit(0);ãassert(0);ã§ãå®é¨ */ return 0; } void func1() { printf( "çµäºä¸...func1\n" ); atexit(func2); }
çµäºå¦çæã«atexitããã¦ã¿ã
#include <stdio.h> #include <stdlib.h> void func1(); void func2(); int main() { atexit(func1); printf( "Enterãã¼ãæ¼ãã¨ããã°ã©ã ãçµäºãã¾ã\n" ); getchar(); return 0; } void func1() { printf( "çµäºä¸...func1\n" ); atexit(func2); } void func2() { printf( "çµäºä¸...func2\n" ); }
ããã¯ãã¡ãªããã§ããçµäºä»¥åã«ç»é²ãããé¢æ°ã®ã¿å®è¡ãããããã§ãã
$ ./atexit
Enterãã¼ãæ¼ãã¨ããã°ã©ã ãçµäºãã¾ãçµäºä¸...func1
ã¾ã¨ã
大åå¤æ°ã§ã¨ãã®ã¡ã¢ãªã®è§£æ¾ã¨ãã«ã¯ä½¿ããããªãï¼ãã¾ã使ããªããã©ã»ã»
ãã¨ã¯ãçµäºæã®ã¡ãã»ã¼ã¸ãåºãã®ã¨ãã«ã¯ä½¿ããï¼ï¼
ã¡ã¢ã¡ã¢