Using exit or return

When you call exit or execute a return statement from main, static objects are destroyed in the reverse order of their initialization. The following example shows how such initialization and cleanup works.

Example

// using_exit_or_return1.cpp
#include <stdio.h>
class ShowData {
public:
   // Constructor opens a file.
   ShowData( const char *szDev ) {
   errno_t err;
      err = fopen_s(&OutputDev, szDev, "w" );
   }

   // Destructor closes the file.
   ~ShowData() { fclose( OutputDev ); }

   // Disp function shows a string on the output device.
   void Disp( char *szData ) { 
      fputs( szData, OutputDev );
   }
private:
   FILE *OutputDev;
};

//  Define a static object of type ShowData. The output device
//   selected is "CON" -- the standard output device.
ShowData sd1 = "CON";

//  Define another static object of type ShowData. The output
//   is directed to a file called "HELLO.DAT"
ShowData sd2 = "hello.dat";

int main() {
   sd1.Disp( "hello to default device\n" );
   sd2.Disp( "hello to file hello.dat\n" );
}

In the preceding example, the static objects sd1 and sd2 are created and initialized before entry to main. After this program terminates using the return statement, first sd2 is destroyed and then sd1. The destructor for the ShowData class closes the files associated with these static objects. (For more information about initialization, constructors, and destructors, see Special Member Functions.)

Another way to write this code is to declare the ShowData objects with block scope, allowing them to be destroyed when they go out of scope:

int main() {
   ShowData sd1, sd2( "hello.dat" );

   sd1.Disp( "hello to default device\n" );
   sd2.Disp( "hello to file hello.dat\n" );
}

See Also

Reference

Additional Termination Considerations