当前位置:网站首页>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.
边栏推荐
- M3SDA:用于多源域自适应的矩匹配
- Day31 LeetCode
- 【PyTorchVideo教程01】快速实现视频动作识别
- Download Win11 how to change the default path?Download Win11 change the default path method
- PHP低代码开发引擎—表单设计
- MindSpore:ImageFolderDataset数据读取问题
- MySQL大总结
- 来了!东方甄选为龙江农产品直播带货
- The advanced version of the cattle brushing series (search for rotating sorted arrays, inversion of the specified range in the linked list)
- What is the difference between a cloud database and an on-premises database?
猜你喜欢
Install MySQL tutorial under Linux
MindSpore:【Resolve node failed】解析节点失败的问题
Linux下安装MySQL教程
MySQL database master-slave configuration
MySQL函数(经典收藏)
DCM 中间件家族迎来新成员
MindSpore:ImageFolderDataset数据读取问题
Database indexes: indexes are not a panacea
Linux download and install mysql5.7 version tutorial the most complete and detailed explanation
推荐系统:评估指标【离线评估指标:RMSE(均方根误差)、AUC、准确率、召回率、F1】【在线评估:A/B测试】【一般要求响应时间<0.5s】
随机推荐
OSS简单上传图片
Download Win11 how to change the default path?Download Win11 change the default path method
看完《二舅》,我更内耗了
MySQL六脉神剑,SQL通关大总结
VBA runtime error '-2147217900 (80040e14): Automation error
MySQL八股文背诵版
win2003下FTP服务器如何搭建
What is the difference between a cloud database and an on-premises database?
.eslintrc.js for musicApp
Alibaba Cloud Martial Arts Headline Event Sharing
MySQL复制表结构、表数据的方法
MindSpore:【MindSpore1.1】Mindspore安装后验证出现cudaSetDevice failed错误
MindSpore:【模型训练】【mindinsight】timeline的时间和实际用时相差很远
Cesium loads offline maps and offline terrain
Interviewer Ali: Describe to me the phenomenon of cache breakdown, and talk about your solution?
ERROR 1045 (28000) Access denied for user 'root'@'localhost'Solution
MySQL性能优化(硬件,系统配置,表结构,SQL语句)
[Private Series] All kinds of strange problems encountered in daily PHP
How architects grow
After MySQL grouping, take the largest piece of data [optimal solution]