当前位置:网站首页>Ordinary int main(){} does not write return 0; what will happen?
Ordinary int main(){} does not write return 0; what will happen?
2022-07-30 20:01:00 【A ray of sunshine a】
The conclusion may be known by looking at the above picture, without adding return 0; the compiler will automatically add one.How to prove it?
You can view the corresponding assembly code, and it is very convenient to use the godbolt.org website to view the assembly code.

As shown above, enter the C++ code, and the assembly code compiled by the compiler will be displayed in the right half. By looking at the assembly instructions behind the high-level language, we can better analyze the performance of the code.The more powerful feature of the website is that it supports almost all compilers on the market, and there are various versions.
Use this website to analyze a wave below!
Look at a screenshot with return 0:

Look at the code circled by the red box on my right. You can understand that eax is the return value of the main() function. Here you can see that the return value of the main() function is 0.
Look at the corresponding screenshot without return 0:

It can be found that the compiler still sets the value of eax to 0, which can be understood as, even if return 0 is not written in the main function, the compiler will still add a return 0 by default.
Will other non-main functions also add a return 0 by default?
Let's take a look at an ordinary function with an int return value:

Note that the eax register in func() is assigned a value of 9, that is, the return value of func() is 9.
What if return a is not added?

It can be seen that if the return statement is not written in func(), the compiler will not update the value of the eax register, and the return value of the function will not be what we expect.
Conclusion: Normally a function with a return value, the return value will exist somewhere, it may be on the stack or in the register. If you don't give it a return value, the return value obtained externally may beA strange value, because we don't know what data that address is.
But, except for the main function, because if the main function does not return, the compiler will add a return 0 by default; but I personally think this is indeed a bad habit, the mian function will make this mistake, and other functions are estimated to beThat's it, so it's better to add return xxx to all functions with return values.
边栏推荐
- MySQL分库分表
- These services can't ali interview?Then don't go to, the basic notification, etc
- MySQL slow query optimization
- MindSpore:对image作normalize的目的是什么?
- Download and installation of the latest version of MySQL 8.0 under Linux (detailed steps)
- Database indexes: indexes are not a panacea
- How do radio waves transmit information?
- 阿里面试这些微服务还不会?那还是别去了,基本等通知
- jOOQ是如何设计事务API(详细指南)
- The technology is very powerful, do you still need to "manage up"?
猜你喜欢

MySql密码

Zabbix 5.0 Monitoring Tutorial (1)

M3SDA: Moment matching for multi-source domain adaptation

MySQL分组后取最大一条数据【最优解】

MySQL performance optimization (hardware, system configuration, table structure, SQL statements)

湖仓一体电商项目(四):项目数据种类与采集

“数字化重构系统,搞定 CEO 是第一步”

Zabbix 5.0 监控教程(一)

These services can't ali interview?Then don't go to, the basic notification, etc

推荐系统:实时性【特征实时性:客户端实时特征(秒级,实时)、流处理平台(分钟级,近实时)、分布式批处理平台(小时/天级,非实时)】【模型实时性:在线学习、增量更新、全量更新】
随机推荐
ELK log analysis system
试写C语言三子棋
Mapped Statements collection does not contain value for的解决方法
推荐系统:实时性【特征实时性:客户端实时特征(秒级,实时)、流处理平台(分钟级,近实时)、分布式批处理平台(小时/天级,非实时)】【模型实时性:在线学习、增量更新、全量更新】
ELK日志分析系统
iPhone真是十三香?两代产品完全对比,或许上一代更值得买
After MySQL grouping, take the largest piece of data [optimal solution]
These services can't ali interview?Then don't go to, the basic notification, etc
MySQl数据库————DQL数据查询语言
[Private Series] All kinds of strange problems encountered in daily PHP
mysql慢查询优化
ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘解决方法
After watching "Second Uncle", I was even more internalized
Win11如何更改默认下载路径?Win11更改默认下载路径的方法
win2003下FTP服务器如何搭建
【PM专用】快速统计团队还有谁没有登记上报信息,快速筛选出属于自己项目组的成员,未完成XXX工作事项的名单
MySQL kills 10 questions, how many questions can you stick to?
【私人系列】日常PHP遇到的各种稀奇古怪的问题
MindSpore:【语音识别】DFCNN网络训练loss不收敛
一文2500字手把手教你配置Jenkins自动化邮件通知