aborb()程序结束形式

abort()与exit()的区别?              

        分类:             MFC             

abort()使程序异常退出,exit()可以有传入值。
--------------------------------------------------------------- abort writes a termination message on stderr ("Abnormal program termination"), then aborts the program by a call to _exit with exit code 3.
exit terminates the calling process. Before termination, exit does the following: closes all files writes buffered output (waiting to be output) calls any registered "exit functions" (posted with atexit) --------------------------------------------------------------- exit()可以指定退出的状态,正常或者出错。 abort()好像只是退出程序而已。 --------------------------------------------------------------- exit()退出前先要执行vatexit()设置的函数 abort()使程序非正常退出 ---------------------------------------------------------------
abort()仅仅使程序退出,啥也不做. exit()在退出前要做一些清理工作. --------------------------------------------------------------- 在华中科技大学《C++程序设计实践教程》中,谈到这一区别: 按面向对象的思想,程序也是一个对象,因此,程序也有生有死。 C++在编译一个程序后,这样执行程序:(1)执行开工函数,此时对 程序初始化,主要指全局变量初始化。(2)调用main函数。(3)执行 收工函数,对全局变量(对象)进行析构。所以如下程序会有输出, 尽管main函数为空: #include <stdio.h> int  x=printf("ABCDEF"); void main( ){ } 现在,来谈abort和exit以及return的区别。return返回,可析构 main或函数中的局部变量,尤其要注意局部对象,如不析构可能造成 内存泄露。exit返回不析构main或函数中的局部变量,但执行收工函数, 故可析构全局变量(对象)。abort不析构main或函数中的局部变量,也不 执行收工函数,故全局和局部对象都不析构。 所以,用return更能避免内存泄露,在C++中用abort和exit都不是好 习惯。 请给分。 ---------------------------------------------------------------
exit() Terminate the calling process after cleanup (exit) or immediately (_exit). abort() Aborts the current process and returns an error code

____________________________________________________________

 #include <stdlib.h>

exit和_exit函数用于正常终止一个程序: _exit立即进入内核,exit则先执行一些清除处理(包括调用执行各终止处理程序,关闭所有标准I / O流等),然后进入内核。使用不同头文件的原因是:exit是由ANSI C说明的,而_exit则是由POSIX.1说明的。        由于历史原因,exit函数总是执行一个标准I/O库的清除关闭操作:对于所有打开流调用 fclose 函数。exit和_exit都带一个整型参数,称之为终止状态(exit status)。大多数UNIX shell都提供检查一个进程终止状态的方法。如果( a )若调用这些函数时不带终止状态,或( b ) main执行了一个无返回值的re turn语句,或( c ) main执行隐式返回,则该进程的终止状态是末定义的。这就意味着,下列经典性的C语言程序:              #include <stdio.h>              main ()              {                       printf ("hello, world /n");              }        是不完整的,因为main函数没有使用return语句返回(隐式返回),它在返回到C的起动例程时并没有返回一个值(终止状态)。另外,若使用:                return( 0 ) ;或者                exit( 0 );         则向执行此程序的进程(常常是一个shell进程)返回终止状态0。另外,main函数的说明实际上应当是:                                              int main(void)        将main说明为返回一个整型以及用exit代替return,对某些C编译程序和UNIX lint(1)程序而言会产生不必要的警告信息,因为这些编译程序并不了解main中的exit与return语句的作用相同。警告信息可能是“ control reaches end of nonvoid function(控制到达非void函数的结束处)”。避开这种警告信息的一种方法是:在main中使用return语句而不是exit。但是这样做的结果是不能用UNIX的grep公用程序来找出程序中所有的exit调用。另外一个解决方法是将main说明为返回void而不是int,然后仍旧调用exit。这也避开了编译程序的警告,但从程序设计角度看却并不正确。本章将main表示为返回一个整型,因为这是ANSIC和POSIX.1所定义的。我们将不理会编译程序不必要的警告

 

 

 

 

 

 

 

 

其实很多人所谓的正常退出,基本上指以下两种: 1. 退出前必须完成指定的工作,如向某个远端的端口发消息 2. 退出时要关照到那些打开的流,如要正常将缓冲区的数据输出到文件
这样考虑的话,异常退出就是上面罗列两点反过来...
我们再来关注程序的几种退出方式: 1. 自然死亡:main函数的return 0 2. 勇敢自杀:调用 exit 或者 abort 3. 死于非命:被杀手CTRL+C/kill,不小心踩到地雷(触发某些硬件错误),中了流弹(未捕获的信号、异常)
硬件错误、无法捕获的异常这两种情况无法把控,所以这里不详述。其他程序退出方式中,可以被归类为异常退出的,也就不外乎这两类: 1. Ctrl+C kill之类,且未完成正常退出时该完成的工作 2. abort exit之类,且未完成正常退出时该完成的工作
换句话说,如果被终止时,已经没有需要做的工作,即使是被kill,也可视作正常退出。这里,让我们关注于符合“异常退出”条件的情况。上面列出的两种异常退出的主要区别: 1. 第一类是不会去清理打开的文件描述符的 2. 第二类会清理文件描述符,缓冲区;abort 和 exit 的不同在于是否析构静态和全局对象,exit还可调用atexit注册回调函数,不过如果对SIGABRT信号注册handler,那abort和exit就没什么区别了。
最后,给出简单的结论,不继续展开了。通过下面的方法可正常退出: 1. 通过abort或者exit配合回调函数,在退出之前完成需要做的清理工作
2. 捕获常见的"杀手",再执行 1。常见需要捕获的有:SIGINT SIGTERM
------- 信号参考资料:http://www.ibm.com/developerworks/cn/linux/l-ipc/part2/index1.html
abort是会产生core文件的 terminate abort exit 的区别...

    • abort indicates "abnormal" end to the program,  and raises the the POSIX signal SIGABRT, which means that any handler  that you have registered for that signal will be invoked, although the  program will still terminate afterwords in either case. Usually you  would use abort in a C program to exit from an unexpected  error case where the error is likely to be a bug in the program, rather  than something like bad input or a network failure. For example, you  might abort if a data structure was found to have a NULL pointer in it when that should logically never happen.

    • exit indicates a "normal" end to the program, although this may still indicate a failure (but not a bug). In other words, you might exit with an error code if the user gave input that could not be parsed, or a file could not be read. An exit code of 0 indicates success. exit also optionally calls handlers before it ends the program. These are registered with the atexit and on_exit functions.

    • std::terminate is what is automatically called  in a C++ program when there is an unhandled exception. This is  essentially the C++ equivalent to abort, assuming that you  are reporting all your exceptional errors by means of throwing  exceptions. This calls a handler that is set by the std::set_terminate function, which by default simply calls abort.

posted @ 2014-12-13 17:41  李艳21  阅读(370)  评论(0编辑  收藏  举报