Thread: the exit() - function

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    3

    Arrow the exit() - function

    Hi out there

    Well, i got a problem in trying to understand what an "exit(1);" in the sourcecode _really_ does when the program is executed (on a Linux-box).
    I read something about this exit(1) would only be a jump address on the stack. Is this right? If so, where exactly is it jumping to? How can I see this address (gdb?)?
    May someone also could point me to some papers (english or german) that can answer these questions - I haven't found the good ones yet.

    thank you all
    regards
    tim

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > what an "exit(1);" in the sourcecode _really_ does when the program is executed (on a Linux-box).
    It causes your program to stop, and return a status of 1 to the environment (usually your shell)

    This you can inspect by typing
    echo $?
    (i think)

    > I read something about this exit(1) would only be a jump address on the stack. Is this right?
    Probably not
    How it is implemented shouldn't really matter - as a programmer in C, you don't need to know such detail.

    > If so, where exactly is it jumping to?
    It might be a call to the operating system to say that the program has now finished.
    Programs do not fall off the bottom of the stack, there is within the run time library an organised way of letting the OS know that the program has ended.

  3. #3
    Sayeh
    Guest
    Actually, it is useful to know such information. Ignorance is the enemy of the professional developer because it forces you to make assumptions that are, more often than not, wrong.

    Normally, when a program exits, it _does_ in fact reach the end of the stack-- that is the stack chain shrinks until the last address returns it to the loader. Once that is accomplished, the stackframe, zero-page information, jump and link info, application heap and process threads (including sub-processes) and thread-interrupts are disposed of (deallocated).

    However, when you use the "exit()" call, to force a premature termination, you force a subsequent jump to the loader irregardless of what state the heap or stack is in. Then the above (previous paragraph) is performed to clean up everything prior to exit.

    Enjoy.

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    3
    Well, thanks a lot, I think this'll help me

    But one more question: when having a closer look to the stack and its content, how can I find or recognize this jump address to the 'end of the stack'?
    In fact 'the end' of the stack is at 0xffffffff but that's probably not what you meant, is it?

    hum, sorry these questions may seem a bit strange, but I'm only trying to really understand what's going on there even (or especally) with the simple programs/functions/calls ect.


    thx&cya
    tim

  5. #5
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    It should be pointed out that a call to exit() causes the function registered by atexit() to execute (prior to process termination). The function _exit() does not bother with whatever may be registered by atexit().

    This applies for POSIX systems; I can only speculate how Windows handles calls to exit().
    Jason Deckard

  6. #6
    Sayeh
    Guest
    There are two stacks.... the processor stack and the program stack for your app. The first address on the program stack is usually at the very end of your heap. stack grows down.

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    3
    hm, can anyone point me to a _good_ text about the stack?

    thx again
    tim

  8. #8
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    "Assembly Language Step by Step" (Duntemann) does a good job of explaining the stack in both real and protected modes.
    Jason Deckard

  9. #9
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    In my opinion, using exit() is a bad programming style. It is always possible to design your algorithms in such a way that things like exit() are not needed. The same for

    Code:
    void function (...)
    {
        ...
    
        if (condition)
            return;
    
        ...
    }
    Such things make your source very hard to read. In my opinion a program is best designed and most safe when it ends at the returning of the return value in the main().

    Code:
    int main (..)
    {
        ....
    
        return return_value;
    }

  10. #10
    ROY
    Guest
    I would like to point out that the function registered by atexit() is executed even when the main function executes return, not necessarily exit().

  11. #11
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    Originally posted by Shiro
    In my opinion, using exit() is a bad programming style. It is always possible to design your algorithms in such a way that things like exit() are not needed. The same for

    Code:
    void function (...)
    {
        ...
    
        if (condition)
            return;
    
        ...
    }
    Such things make your source very hard to read. In my opinion a program is best designed and most safe when it ends at the returning of the return value in the main().

    Code:
    int main (..)
    {
        ....
    
        return return_value;
    }
    Hmmm, ironic. One of my lecturers always uses early returns as he HATES the else clause. But we suspect him of alcoholism, so...
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Odd memory leaks
    By VirtualAce in forum C++ Programming
    Replies: 11
    Last Post: 05-25-2006, 12:56 AM
  3. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 09:23 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM