当前位置:网站首页>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.
边栏推荐
- 如何优化OpenSumi终端性能?
- 【无标题】多集嵌套集合使不再有MultipleBagFetchException
- 推荐系统:冷启动问题【用户冷启动、物品冷启动、系统冷启动】
- MySQL eight-part text recitation version
- Day31 LeetCode
- OSS简单上传图片
- Interviewer Ali: Describe to me the phenomenon of cache breakdown, and talk about your solution?
- MindSpore:【resnet_thor模型】尝试运行resnet_thor时报Could not convert to
- Linux下安装MySQL教程
- MySQL performance optimization (hardware, system configuration, table structure, SQL statements)
猜你喜欢

银行数据资产转换能力弱?思迈特软件助力解决银行困境

推荐系统-排序层:排序层架构【用户、物品特征处理步骤】

MindSpore:【resnet_thor模型】尝试运行resnet_thor时报Could not convert to

The advanced version of the cattle brushing series (search for rotating sorted arrays, inversion of the specified range in the linked list)

ERROR 1045 (28000) Access denied for user 'root'@'localhost'Solution

MySQl数据库————DQL数据查询语言

PHP低代码开发引擎—表单设计

The JDBC programming of the MySQL database

ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘解决方法

已删除
随机推荐
MySQL Functions (Classic Collection)
MySQl数据库————DQL数据查询语言
Install MySQL tutorial under Linux
MySQL slow query optimization
musicApp 的.eslintrc.js
After watching "Second Uncle", I was even more internalized
360杜跃进:太空安全风险加剧,需打造一体化防御体系
linux下mysql8安装
MySQL六脉神剑,SQL通关大总结
HCIP --- 企业网的三层架构
MySQL复制表结构、表数据的方法
Download and installation of the latest version of MySQL 8.0 under Linux (detailed steps)
MySQL大总结
时间复杂度与空间复杂度
Different lower_case_table_names settings for server (‘1‘) and data dictionary (‘0‘) 解决方案
MindSpore:【MindSpore1.1】Mindspore安装后验证出现cudaSetDevice failed错误
MindSpore:mindspore有没有类似tf.GradientTape()用来求解梯度的?
coming!Dongfang Selection brings goods to the live broadcast of Longjiang agricultural products
LeetCode 0952. Calculate Maximum Component Size by Common Factor: Mapping / Union Search
How to install and use PostgreSQL 14.4