如果主()不返回一个int值会发生什么? [英] What happens if main() does not return an int value?

查看:111
本文介绍了如果主()不返回一个int值会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,在C编译器的的main()功能由具有 _start()函数调用code是这样的:

I know that in C compilers the main() function is called by the _start() function which has code something like this:

exit(main()); // return value of main is returned

如何 _start()工作的时候的main()不返回 INT ,例如,如果它的返回类型为无效浮动,还是其他什么东西?

How does _start() work when main() does not return int, for example if its return type is void, float, or something else?

推荐答案

如果不返回 INT ,那么你有一个病态的程序和行为是不确定的。任何事情都有可能发生。您的程序可能会崩溃,或者,好象什么错都可能运行。

If main doesn't return int, then you have an ill-formed program and behavior is undefined. Anything can happen. Your program might crash, or it might run as though nothing were wrong at all.

让我们假设返回比 INT 其他的东西,你的编译器和连接器允许进行该计划。调用者不知道,虽然。如果预计,返回调用者 INT 来的EAX(英特尔)寄存器来返回值,那么这就是它会读取以确定的返回值主。如果您有问题保存在浮动价值存在,那么这将是PTED为<$ C间$ P $ $ C> INT 来代替。 (这并不意味着它会被截断。这意味着比特构成一个浮点值的布局,而不是将构成一个 INT 来代替。)如果您有问题返回无效,那么它并没有存储在预期的寄存器什么,所以调用者会得到什么价值previously存储在该寄存器来代替。

Let's suppose main returned something other than int, and your compiler and linker allowed the program to be made. The caller doesn't know that, though. If the caller expects returned int values to be returned in the EAX (Intel) register, then that's what it will read to determine the return value of main. If your faulty main stored a float value there, then it will be interpreted as an int instead. (That doesn't mean it will get truncated. It means the bits making up the layout of a floating-point value will instead make up an int instead.) If your faulty main returned void, then it didn't store anything in the expected register, so the caller will get whatever value was previously stored in that register instead.

如果你的返回某种类型,预计存储某处调用者没有'储备存储器(如大型结构),那么它就会结束覆盖了别的东西,或许是重要的程序的干净关闭,导致程序崩溃。

If your main returns some type that it expects to store someplace that the caller didn't' reserve memory for (such as a large struct), then it will end up overwriting something else, perhaps something important to the clean shutdown of the program, causing your program to crash.

这篇关于如果主()不返回一个int值会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆