结束程序的代码return 0 exit(0) ExitProcess(0)的不同之处

退出程序的几种方式

1、首先说下程序是如何启动的,前面的读书笔记已经详细写了,这儿再重复下

程序执行的步骤:

A、 检索指向新进程的完整命令行的指针;

B、 检索指向新进程的环境变量的指针;

C、 对C/C++运行期的全局变量进程初始化;

D、 对C运行期间内存单元分配函数和其他底层输入/输出例程使用的内存栈进程初始化;

E、  为所有全局和静态C++类对象调用构造函数

F、  进入接入点函数,执行、退出                         

G、 调用由_onexit函数的调用而注册的任何函数

H、 为所有全局和静态C++类对象调用析构函数;

I、   调用操作系统的ExitProcess函数

 

2、有如下程序:

[cpp]  view plain copy
  1. <span style="font-size:16px;">class CSomeObj  
  2. {  
  3. public:  
  4.     CSomeObj()   
  5.     {  
  6.         printf("Constructor\r\n");  
  7.     }  
  8.     ~CSomeObj()  
  9.     {  
  10.         printf("Destructor\r\n");  
  11.     }  
  12. };  
  13.   
  14.   
  15. CSomeObj g_GlobalObj;  
  16.   
  17. int _tmain(int argc, _TCHAR* argv[])  
  18. {  
  19.     CSomeObj LocalObj;  
  20.   
  21.     ExitProcess(0);  
  22.     //exit(0);  
  23.     return 0;  
  24. }</span>  

调用 ExitProcess(0)时,发现输出为:

Constructor

Constructor

调用exit(0)时,输出为:

Constructor

Constructor
Destructor

调用return 0 时,输出为:

Constructor

Constructor
Destructor

Destructor

 

其实分析下应该知道:

在Windows下,return 0 的实际执行过程是:

1、先析构main函数内的局部对象,返回至调用main的函数。
2、调用exit函数,由exit函数调用doexit函数,在doexit函数中完成对全局对象的析构。
3、最后调用ExitProcess结束进程。


所以总结下:

ExitProcess不负责任何对象的析构;exit只负责析构全局对象;return 0可以析构局部对象并调用exit,因此能析构全部对象。

ExitProcess是退出代码的意思,其中ExitProcess(0)表示正常退出,ExitProcess(1)表示错误退出。退出代码是给你的父进程或调试你的进程的进程用的,对你的进程来说,用退出代码可以告诉你的父进程或调试进程你的运行结果,是正常不是不正常、出现了什么错误,等等的信息。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个汇编程序是一个简单的示例,演示了如何在汇编语言中实现加法运算和退出程序。 ``` ; Example assembly language program -- adds two numbers ; Author: R. Detmer ; Date: revised 7/97 .386 .MODEL FLAT ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD INCLUDE io.h cr EQU 0dh ; carriage return character Lf EQU 0ah ; line feed .STACK 4096 .DATA a DD 5 ; first number b DD 7 ; second number result DD ? ; result of addition .CODE _start: mov eax, a ; move first number into EAX register add eax, b ; add second number to EAX register mov result, eax ; store result in memory print_str "The sum of ", a, " and ", b, " is ", result, cr, Lf INVOKE ExitProcess, 0 ; exit with return code 0 print_str PROC STDCALL push ebp mov ebp, esp pushad mov edx, 0 .print_loop: mov eax, [ebp + 8 + edx * 4] cmp eax, 0 je .print_end push eax call print_int add esp, 4 inc edx jmp .print_loop .print_end: mov eax, [ebp + 8 + edx * 4] cmp eax, cr je .print_return cmp eax, Lf je .print_return push eax call print_char add esp, 4 jmp .print_end .print_return: popad mov esp, ebp pop ebp ret print_str ENDP print_char PROC STDCALL push ebp mov ebp, esp push ebx mov bl, byte ptr [ebp + 8] mov eax, 1 mov ecx, esp mov [ecx], bl mov ebx, 0 mov edx, 1 int 0x80 pop ebx mov esp, ebp pop ebp ret print_char ENDP print_int PROC STDCALL push ebp mov ebp, esp push ebx push esi push edi mov eax, [ebp + 8] mov ebx, 10 xor edx, edx mov esi, esp cmp eax, 0 jge .int_positive neg eax mov ah, '-' mov byte ptr [esi], ah inc esi .int_positive: cmp eax, 0 jne .int_loop mov byte ptr [esi], '0' inc esi jmp .int_end .int_loop: xor edx, edx div ebx add dl, '0' mov byte ptr [esi], dl inc esi cmp eax, 0 jne .int_loop .int_end: mov byte ptr [esi], 0 mov edi, esi dec esi mov ebx, esp .int_reverse: cmp esi, edi jge .int_return mov al, byte ptr [esi] mov byte ptr [esi], byte ptr [edi] mov byte ptr [edi], al dec edi inc esi jmp .int_reverse .int_return: push ebx call print_str add esp, 4 pop edi pop esi pop ebx mov esp, ebp pop ebp ret print_int ENDP PUBLIC _start END ``` 这个程序使用了 IO.h 头文件中的 PRINT_STRING, PRINT_CHAR 和 PRINT_INT 函数来打印输出。程序将两个数字相加,并将结果存储在 memory 中,然后将结果打印到控制台。最后,程序调用 ExitProcess 函数退出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值