当前位置:网站首页>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.
边栏推荐
- After watching "Second Uncle", I was even more internalized
- MySQL kills 10 questions, how many questions can you stick to?
- How to copy table structure and table data in MySQL
- 360杜跃进:太空安全风险加剧,需打造一体化防御体系
- 牛客网——华为题库(100~108)
- centos7安装mysql8
- MindSpore: CV.Rescale(rescale,shift)中参数rescale和shift的含义?
- The advanced version of the cattle brushing series (search for rotating sorted arrays, inversion of the specified range in the linked list)
- linux下mysql8安装
- Perfectly Clear QuickDesk & QuickServer图像校正优化工具
猜你喜欢
随机推荐
OSS简单上传图片
MySQL函数(经典收藏)
Day31 LeetCode
055 c# print
推荐系统-排序层:排序层架构【用户、物品特征处理步骤】
银行数据资产转换能力弱?思迈特软件助力解决银行困境
推荐系统:实时性【特征实时性:客户端实时特征(秒级,实时)、流处理平台(分钟级,近实时)、分布式批处理平台(小时/天级,非实时)】【模型实时性:在线学习、增量更新、全量更新】
el-input can only input integers (including positive numbers, negative numbers, 0) or only integers (including positive numbers, negative numbers, 0) and decimals
Zabbix 5.0 监控教程(一)
MySQL eight-part text recitation version
mysql8 installation under linux
推荐系统-模型:FNN模型(FM+MLP=FNN)
Linux下安装Mysql5.7,超详细完整教程,以及云mysql连接
Encapsulates a console file selector based on inquirer
Start background services across processes
MindSpore: CV.Rescale(rescale,shift)中参数rescale和shift的含义?
MySQL Functions (Classic Collection)
Snowflake vs. Redshift的2022战报:两个数据平台谁更适合你?
MySQl数据库————DQL数据查询语言
【MindSpore】用coco2017训练Model_zoo上的 yolov4,迭代了两千多batch_size之后报错,大佬们帮忙看看。









