Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use atexit() function to cleanup function call? [duplicate]

Tags:

c

exit

atexit

I am using atexit() function inside my code to cleanup function call, but it's not working.

#include<stdio.h>
void ftn(void)
{
    printf(" Function called --> exit\n");
    return;
}
int main(void)
{
    int x = 0;
    atexit(ftn);
    for(;x<0xffffff;x++);
    _exit(0);
}

Any help regarding this will be appreciated.

like image 817
AR.5 Avatar asked Sep 16 '25 15:09

AR.5


1 Answers

This behavior of atexit() function is due to the use of function _exit(). This function does not call the clean-up functions like atexit() etc. If atexit() is required to be called then exit() or ‘return’ should be used instead of _exit().

As:

#include<stdio.h>
void ftn(void)
{
    printf(" Function called --> exit\n");
    return;
}
int main(void)
{
    int x = 0;
    atexit(ftn);
    for(;x<0xffffff;x++);
    exit(0);
}
like image 100
abbasi_ahsan Avatar answered Sep 19 '25 06:09

abbasi_ahsan