本文介绍了main()在运行时异常返回什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当程序遇到运行时异常(例如分段错误)时,C int main()函数返回什么?

在寻找答案时,我遇到了许多有关 main()应该返回,就像这样.

解决方案

如果程序崩溃,它(main函数)不会返回.如果程序崩溃,则操作系统将终止该程序,因此该程序不再运行,并且包含main函数.不运行的程序无法自行返回任何内容.返回到运行环境的内容取决于操作系统,该操作系统已在程序之后接管.

无论返回什么,都由操作系统处理.对于POSIX系统,操作系统通过信号(例如SIGSEGV,分段错误)终止的进程将返回128加上信号号.这在例如waitpid参考页(及其链接).

对于Windows,通常将其报告为神秘的long值(通常为0x80000000值以及其他标志和数据).

对于不处理崩溃的较旧或更原始的操作系统,返回"的值通常是崩溃时在返回值"寄存器中或堆栈顶部的值. /p>

What does the C int main() function return when the program hits a run-time exception (e.g. segmentation fault)?

When searching for an answer I hit many discussions/posts about what main() should return, like this one.

解决方案

It (the main function) doesn't return if the program crashes. If a program crashes, then the operating system would have killed the program, so the program isn't running anymore and that includes the main function. A program that doesn't run can't return anything on its own. What is "returned" to the running environment depends on the operating system, which have taken over after the program.

Whatever is returned is handled by the operating system. For POSIX systems, a process that is killed by a signal (like SIGSEGV, segmentation fault) the OS will return 128 plus the signal number. This is documented in e.g. this waitpid reference page (and the links from it).

For Windows it's typically reported as a cryptic long value (usually the value 0x80000000 plus other flags and data).

For older or more primitive operating systems that don't handle crashes, the value being "returned" is usually what happen to be in the "return value" register or on top of the stack at the time of the crash.

这篇关于main()在运行时异常返回什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 13:09