Differences and analysis of Return,exit and exitprocess under Windows

Source: Internet
Author: User

In general, we will use return or call exit () in the main function in order to end our own program. There are also functions such as exitprocess () and TerminateProcess () under Windows.

The purpose of this article is to compare the different ways of ending the program and to analyze its principle.

First, we use an example to illustrate the difference between several end-ways.

The test code is as follows:
#include
#include
#include

Class Test
{
Public
Test (int i) {m_i=i; printf ("Construct%d\n", m_i);};
~test () {printf ("Destruct%d\n", m_i);};
Private
int m_i;
};

Test t_1 (1);

int main (int argc, char* argv[])
{
Test t_2 (2);
printf ("Hello world!\n");
return 0;
Exit (0);
ExitProcess (0);
}

Our goal is to see what the difference is between the two ways of ending.

The result of the program running is:

At the end of using return 0:
Construct 1
Construct 2
Hello world!
Destruct 2
Destruct 1

At the end of Use Exit (0):
Construct 1
Construct 2
Hello world!
Destruct 1

At the end of the use of ExitProcess (0):
Construct 1
Construct 2
Hello world!

As we can see from the results, using return to end the process can correctly deconstruct global and local objects. When you end a process with exit (), the global object can be properly refactored, but the local object is not properly refactored. At the end of ExitProcess (0), both global and local objects are not properly deconstructed.

Why is there such a situation?
In Windows core programming we can get the following explanations:
"When the entry point function of the main thread (WinMain, wWinMain, Main, or wmain) returns, it returns to the C + + run-time startup code, which correctly knows all C run-time resources used by the process. When the C run-time resource is freed, the C run-time startup code explicitly calls ExitProcess and passes the value returned by the entry point function to it. "

Under Windows, the actual execution of return 0 is:

    • First, the local object inside the main function is reconstructed.

    • Return to the function that called Main.

    • Call the Exit function, call the Doexit function by the Exit function, and complete the destruction of the global object in the Doexit function.

    • Finally call ExitProcess to end the process.

Therefore, ExitProcess is not responsible for the destruction of any object, exit is only responsible for the destruction of the global object, return 0 can be the destruction of local objects and call exit, so that all objects can be destructor.

Differences and analysis of Return,exit and exitprocess under Windows

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.