Thread: atexit

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    183

    atexit

    can anyone how atexit works coz i couldnt rlly understand how it goes thanks
    its lil bit confusing

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by lolguy View Post
    can anyone how atexit works coz i couldnt rlly understand how it goes thanks
    its lil bit confusing
    Hey I just copied this from the GNU Ref Manual:
    Code:
         #include <stdio.h>
         #include <stdlib.h>
         
         void
         bye (void)
         {
           puts ("Goodbye, cruel world....");
         }
         
         int
         main (void)
         {
           atexit (bye);
           exit (EXIT_SUCCESS);
         }
    I imagine the purpose is to close files, network connections, child processes, save data, and take care of anything else that might otherwise be left running or in an undefined state after the main process ends. You can do something similar (and perhaps more effectively) with a signal handler.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Basically, it works like this:
    Code:
    // inside your C-library:
    ...
        main();
    ...
        if (atexit_function) atexit_function();
    ...
    atexit itself looks something like this:
    Code:
    void (*atexit_function)(void) = NULL;
    void atexit(void (*func)(void))
    {
       atexit_function = func;
    }
    In real life it's a bit more complex, because there can be several atexit functions, so it's more like an array than a single pointer - but in essence that's how it works.

    The function exit() will make the program jump to the point just after main (or something that has that effect).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    so u just call it inside a function and it exits that function ?
    but why does it run in reverse mode ?

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    static void double_it()
    {
    	int x=2;
    	x*=x;
    	printf("%d",x);
    }
    int main(void)
    {
    atexit(double_it);
    return getchar();
    }
    so it just exit by that function ? or there is more to it ?

  6. #6
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    sorry 1 more question
    main returns getchar first ? or atexit doubles first ?

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by lolguy View Post
    sorry 1 more question
    main returns getchar first ? or atexit doubles first ?
    atexit() is called AFTER main returns.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    oh okay thanks so it the last thing the program runs ty for help i understand it now

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by lolguy View Post
    oh okay thanks so it the last thing the program runs ty for help i understand it now
    Ok, so technically there may well be stuff that happens AFTER atexit(), but that is PURELY part of the C runtime, not something you can change. Just like the C runtime runs code BEFORE main() that you also do not have any way to influence [unless you take the C runtime source code and modify it].

    As an example, after atexit(), any memory allocated through malloc() and friends may be given back to the OS (assuming the OS doesn't do this by itself).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. atexit function
    By strokebow in forum C Programming
    Replies: 2
    Last Post: 11-24-2006, 04:45 PM
  2. Odd memory leaks
    By VirtualAce in forum C++ Programming
    Replies: 11
    Last Post: 05-25-2006, 12:56 AM
  3. atexit not running?
    By ichijoji in forum C++ Programming
    Replies: 4
    Last Post: 08-05-2005, 03:28 PM
  4. how to use atexit()??
    By spike232 in forum C Programming
    Replies: 1
    Last Post: 03-16-2002, 09:40 AM