atexit和onexit这两个C函数,主要的区别是什么?

iuoewrw 2011-04-18 11:05:30
有么有哪个函数是旧函数不推荐使用的? 兼容性,平台支持上等方面,VC对他们有什么考量么?
...全文
662 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
iuoewrw 2011-04-18
  • 打赏
  • 举报
回复
还是说atexit是标准C函数,而onexit是VC的一个扩展?
有确切的说法么?
pathuang68 2011-04-18
  • 打赏
  • 举报
回复
atexit是标准C中的函数,建议使用这个。
onexit是非标准的。有些平台上不存在这个函数。
赵4老师 2011-04-18
  • 打赏
  • 举报
回复
atexit
Processes the specified function at exit.

int atexit( void ( __cdecl *func )( void ) );

Routine Required Header Compatibility
atexit <stdlib.h> ANSI, Win 95, Win NT


For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version


To generate an ANSI-compliant application, use the ANSI-standard atexit function (rather than the similar _onexit function).

Return Value

atexit returns 0 if successful, or a nonzero value if an error occurs.

Parameter

func

Function to be called

Remarks

The atexit function is passed the address of a function (func) to be called when the program terminates normally. Successive calls to atexit create a register of functions that are executed in LIFO (last-in-first-out) order. The functions passed to atexit cannot take parameters. atexit and _onexit use the heap to hold the register of functions. Thus, the number of functions that can be registered is limited only by heap memory.

Example

/* ATEXIT.C: This program pushes four functions onto
* the stack of functions to be executed when atexit
* is called. When the program exits, these programs
* are executed on a "last in, first out" basis.
*/

#include <stdlib.h>
#include <stdio.h>

void fn1( void ), fn2( void ), fn3( void ), fn4( void );

void main( void )
{
atexit( fn1 );
atexit( fn2 );
atexit( fn3 );
atexit( fn4 );
printf( "This is executed first.\n" );
}

void fn1()
{
printf( "next.\n" );
}

void fn2()
{
printf( "executed " );
}

void fn3()
{
printf( "is " );
}

void fn4()
{
printf( "This " );
}


Output

This is executed first.
This is executed next.


Process and Environment Control Routines

See Also abort, exit, _onexit
------------------------------------------------------------------------
_onexit
Registers a routine to be called at exit time.

_onexit_t _onexit( _onexit_t func );

Routine Required Header Compatibility
_onexit <stdlib.h> Win 95, Win NT


For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version


Return Value

_onexit returns a pointer to the function if successful, or NULL if there is no space to store the function pointer.

Parameter

func

Pointer to function to be called at exit

Remarks

The _onexit function is passed the address of a function (func) to be called when the program terminates normally. Successive calls to _onexit create a register of functions that are executed in LIFO (last-in-first-out) order. The functions passed to _onexit cannot take parameters.

_onexit is a Microsoft extension. For ANSI portability use atexit.

Example

/* ONEXIT.C */

#include <stdlib.h>
#include <stdio.h>

/* Prototypes */
int fn1(void), fn2(void), fn3(void), fn4 (void);

void main( void )
{
_onexit( fn1 );
_onexit( fn2 );
_onexit( fn3 );
_onexit( fn4 );
printf( "This is executed first.\n" );
}

int fn1()
{
printf( "next.\n" );
return 0;
}

int fn2()
{
printf( "executed " );
return 0;
}

int fn3()
{
printf( "is " );
return 0;
}

int fn4()
{
printf( "This " );
return 0;
}


Output

This is executed first.
This is executed next.


Process and Environment Control Routines

See Also atexit, exit

70,037

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧