Code to restart/reload program

This is a discussion on Code to restart/reload program within the C++ Programming forums, part of the General Programming Boards category; I am looking for a command that will restart/reload the program. Any help is appreciated. Also, code to end the ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    4

    Code to restart/reload program

    I am looking for a command that will restart/reload the program. Any help is appreciated.

    Also, code to end the program would be nice as well.
    Last edited by GanonsSpirit; 09-14-2007 at 04:47 PM. Reason: Added info.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Just to be sure: do you really want to restart the program or do you simply want to do whatever the program is doing one more time? If the latter, use a looping structure to start execution all over.

    To end program, use return in main and exit(n) (I think) in other functions.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    You can probably use system(MyProg.exe) to start the program.

    If you wanted the program to re-start itself... Well, the program can't do anything after it ends and exits... You could have the program start a 2nd instance of itself, and then end the 1st instance. (Assuming that you are running Windows or another multitasking operating system that would allow you to run two instances of the same program at the same time... And, assuming there's nothing about the program that prevents two instances of it running.)

    anon is right. In most cases, it's better to use a loop. Many programs are written with almost everything inside one big main loop. Inside that loop, you might find more "nested" loops, and several if-statements.

    If you are new to programming, I suggest working through all of the tutorials before trying anything new.
    Last edited by DougDbug; 09-14-2007 at 05:58 PM.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    To end program, use return in main and exit(n) (I think) in other functions.
    Right, exit() from <cstdlib>. Pass it an int, which is the return value, just like from main(). Usually you'd use exit(0) (or exit(EXIT_SUCCESS), or maybe exit(1) (or exit(EXIT_FAILURE)) on error.

    As mentioned, from main, you can just use a return statement.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    In posix you can use
    Code:
           
    #include <stdlib.h>
    
           int atexit(void (*function)(void));
    To set the exit handler which is called on process termination. maybe you can pass it your main function to allow your app to resurrect itself all the time. But main has the signature int(*)(void), so it would be a bad hack if it works at all. Better use on step of indirection: write a function
    Code:
    void handler()
    {
        main();
    }
    I've never tried this...
    Last edited by pheres; 09-16-2007 at 02:00 AM.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Calling main() or taking its address is forbidden by the C++ standard, even if no compiler I know enforces this.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    atexit() is ANSI standard as well as POSIX.

    Besides, you'd end up with code like this:
    Code:
    #include <stdlib.h>
    
    void loop(void) {
        atexit(loop);
    
        if(condition) exit(0);
    }
    
    int main() {
        loop();
    
        return 0;
    }
    So why not call loop() directly? It would be the same thing.

    Also, it would be hard to guarantee that the code would not call exit() again, and both ANSI and POSIX specify that if exit() is called from within a function registered by atexit(), the result is undefined. So with that code, you'd only be able to restart the program once.

    Not to mention that it's a recursive solution so that you'd probably* eventually run out of stack space eventually.

    * Some compilers can optimise recursive code into iterative code.

    In short, that is a very bad idea.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Ok, I probably eventually got it eventually thx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need the code of few programs in c++.plzzzzz help...plzzz
    By NAVINKR20 in forum C++ Programming
    Replies: 1
    Last Post: 05-08-2009, 09:13 AM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  3. large program code ,please help
    By Ash1981 in forum C Programming
    Replies: 14
    Last Post: 01-30-2006, 05:16 AM
  4. Run A Program from within a cpp code
    By Hexxx in forum C++ Programming
    Replies: 6
    Last Post: 01-02-2006, 07:05 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21