Thread: atexit() question.

  1. #1
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288

    atexit() question.

    I was looking at atexit() and saw it takes a function pointer to a function with no return value or parameters. If I wanted to use this to clean up memory I allocated or save any data( like saving highscores to a file for a game ) in some variable, could I do it without the use of global variables?

    Note : Assume I would only register the function after I checked everything is in working order.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > could I do it without the use of global variables?
    No.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Quote Originally Posted by Salem View Post
    > could I do it without the use of global variables?
    No.
    That's too bad. Oh, well, I figured that was the answer, I just wasn't sure if there was a fancy way to get around it. I do have a follow up question though, when I run this code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    
    
    void func( )
    {
        fprintf(stdout, "Yay!\n");
        getchar();
    
    
        return;
    }
    
    
    int main( )
    {
        if ( atexit(func) )
            return 1;
    
    
        Sleep(1000);
    
    
        return 0;
    }
    then it pauses for a while when I press the [x] button before exiting from main. What is the reason for that pause, and does it have to wait for main() to complete before exiting the program?
    Last edited by HelpfulPerson; 08-03-2013 at 03:37 PM.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The pause is from Sleep.

  5. #5
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Quote Originally Posted by whiteflags View Post
    The pause is from Sleep.
    If I exited during some operations in main without the Sleep() function, would it call the function immediately with no pause then?

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You can always return from main before Sleep is executed, if that's what you mean. The trouble is Sleep has probably executed by the time you exited the console.

    This function causes a thread to relinquish the remainder of its time slice and become unrunnable for an interval based on the value of dwMilliseconds [the argument].
    Sleep function (Windows)

    If it's unrunnable, anything the user does won't be processed for a while.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    I think you are letting go of your original question too easily.

    Yes, it's necessary to use a global variable (or worse) to pass data to an atexit() function, but can you think of any way this situation might be improved? You can't eliminate the global variable but you can certainly do better than a random mish-mash of global variables for all the various atexit() functions you want to register. Can you propose a more structured solution to the problem?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Quote Originally Posted by brewbuck View Post
    I think you are letting go of your original question too easily.

    Yes, it's necessary to use a global variable (or worse) to pass data to an atexit() function, but can you think of any way this situation might be improved? You can't eliminate the global variable but you can certainly do better than a random mish-mash of global variables for all the various atexit() functions you want to register. Can you propose a more structured solution to the problem?
    What I would likely do is store the needed data either in a global structure or in a global array. Then I would only copy data from the main program to that global variable when needed, instead of using it throughout the program.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by HelpfulPerson View Post
    That's too bad. Oh, well, I figured that was the answer, I just wasn't sure if there was a fancy way to get around it.
    you give up too easily. Salem was being only partially serious.

    A good way to do this might be to have your "global" data marked as static in one of your source files, and have that same source file contain the functions to read and write that global data. static objects at the global scope are only visible within the source file in which they are declared, so it's not actually global, and won't have potential conflicts with other global data, and writing functions to access the data, instead of manipulating it directly, is always a good idea.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-23-2011, 09:00 AM
  2. atexit
    By lolguy in forum C Programming
    Replies: 8
    Last Post: 01-16-2009, 06:30 PM
  3. atexit function
    By strokebow in forum C Programming
    Replies: 2
    Last Post: 11-24-2006, 04:45 PM
  4. atexit not running?
    By ichijoji in forum C++ Programming
    Replies: 4
    Last Post: 08-05-2005, 03:28 PM
  5. how to use atexit()??
    By spike232 in forum C Programming
    Replies: 1
    Last Post: 03-16-2002, 09:40 AM