zoukankan      html  css  js  c++  java
  • C++一道面试题(atexit)

    题:main主函数执行完毕后,是是否可能会再执行一段代码?给出说明。

    答:如果需要加入一段在main退出后执行的代码,可以使用atexit()函数注册一个函数,代码如下:

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

    int atexit(void(*function)(void));
    void fn1(void),fn2(void),fn3(void),fn4(void);

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

    return 0;
    }

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

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

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

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

    执行结果:

    This is executed first.
    This is executed next.

    说明:

    (1)函数原型:int atexit( void ( __cdecl *func )( void ) );

    (2)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.



  • 相关阅读:
    Android变化如何破解几场金
    mysql 在创建批处理脚本日志表信息
    近期感悟要多说多想多做
    Spring使用小结2
    structs2使用小结2
    2013第50周五打包
    2013第50周四开发记
    jquery使用总结
    2013第50周三开发记
    eclipse编辑工具小结
  • 原文地址:https://www.cnblogs.com/danshui/p/2391315.html
Copyright © 2011-2022 走看看