Run application continuously

c++performance

Whats the smartest way to run an application continuously so that it doesn't exit after it hits the bottom? Instead it starts again from the top of main and only exits when commanded. (This is in C)

Best Solution

You should always have some way of exiting cleanly. I'd suggest moving the code off to another function that returns a flag to say whether to exit or not.

int main(int argc, char*argv[])
{

     // param parsing, init code

     while (DoStuff());

    // cleanup code
    return 0;
 }

 int DoStuff(void)
 {
     // code that you would have had in main

     if (we_should_exit)
         return 0;

     return 1;
 }