C++中的return和exit区别
在main函数中,return和exit经常混用,两者的一个区别:return会执行statck unwinding,而exit不会。如果触发了信号,exit也同样不会做stack unwinding,除此之外异常如果没有相应的catch,也同样不会有栈展开(stack unwinding)。
原因是C++编译器只会在遇到“}”或“return”时,才会安插栈展开代码,对于exit等则没这回事。
#include <signal.h> #include <stdio.h> #include <stdlib.h>
class X { public: X(int m): _m(m) { printf("X::ctor:%d\n", m); } ~X() { printf("X::dtor:%d\n", _m); }
private: int _m; };
int main() { X x(1); #if USE_EXIT exit(0); #if USE_RAISE raise(SIGSEGV); #else return 0; #endif } |
以上述代码为例,通过汇编,可很容易看出这两者的区别:
1) return代码
int main() { X x(1); return(0); } |
反汇编main函数,可以看到有调用~X:
0x08048474 <main+0>: lea 0x4(%esp),%ecx 0x08048478 <main+4>: and $0xfffffff0,%esp 0x0804847b <main+7>: pushl 0xfffffffc(%ecx) 0x0804847e <main+10>: push %ebp 0x0804847f <main+11>: mov %esp,%ebp 0x08048481 <main+13>: push %ebx 0x08048482 <main+14>: push %ecx 0x08048483 <main+15>: sub $0x20,%esp 0x08048486 <main+18>: movl $0x1,0x4(%esp) 0x0804848e <main+26>: lea 0xfffffff4(%ebp),%eax 0x08048491 <main+29>: mov %eax,(%esp) 0x08048494 <main+32>: call 0x80484b6 <X> 0x08048499 <main+37>: mov $0x0,%ebx 0x0804849e <main+42>: lea 0xfffffff4(%ebp),%eax 0x080484a1 <main+45>: mov %eax,(%esp) 0x080484a4 <main+48>: call 0x80484da <~X> 0x080484a9 <main+53>: mov %ebx,%eax 0x080484ab <main+55>: add $0x20,%esp 0x080484ae <main+58>: pop %ecx 0x080484af <main+59>: pop %ebx 0x080484b0 <main+60>: pop %ebp 0x080484b1 <main+61>: lea 0xfffffffc(%ecx),%esp 0x080484b4 <main+64>: ret |
2) exit代码
int main() { X x(1); exit(0); } |
反汇编main函数,可以看到没有调用~X:
0x080484a4 <main+0>: lea 0x4(%esp),%ecx 0x080484a8 <main+4>: and $0xfffffff0,%esp 0x080484ab <main+7>: pushl 0xfffffffc(%ecx) 0x080484ae <main+10>: push %ebp 0x080484af <main+11>: mov %esp,%ebp 0x080484b1 <main+13>: push %ecx 0x080484b2 <main+14>: sub $0x24,%esp 0x080484b5 <main+17>: movl $0x1,0x4(%esp) 0x080484bd <main+25>: lea 0xfffffff8(%ebp),%eax 0x080484c0 <main+28>: mov %eax,(%esp) 0x080484c3 <main+31>: call 0x80484d4 <X> 0x080484c8 <main+36>: movl $0x0,(%esp) 0x080484cf <main+43>: call 0x80483c8 <exit@plt> |
附1:137
进程退出后“echo $?”的值含义:一般 0 表示成功;如果被信号干掉的,则“$? - 128”后的值为信号值,比如“137 - 128”后为 9,即表示收到了 SIGKILL 信号。
附2:汇编指令
call指令 |
分两步: 1) 将当前的IP或CS和IP压入栈中 2) 转移(能实现短转移,它的书写格式同jmp指令) |
ret指令 |
相当于pop IP |
retf指令 |
相当于: 1) pop IP 2) pop CS |
lea指令 |
把操作数OPRD的偏移地址传送到寄存器REG,语法:LEA REG, OPRD |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义