欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C 语言中的constructor与destructor c编译器GCCconstructordestructor

程序员文章站 2024-03-06 18:53:56
...
   最近在看代码的时候碰到一个问题,这项目中有一个全局变量,里面有许多系统用的属性。但是一直找不到它在哪里被赋值。后来跟了代码才发现在系统开始之前已经有一个constructor将这个东西初始化好。
   GCC可以给函数若干属性,其中construction就是其中一个。具体有哪些属性,可以看GCC的文档。http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
   在上面文档中有对于constructor与destructor的描述:
引用

constructor
destructor
constructor (priority)
destructor (priority)
The constructor attribute causes the function to be called automatically before execution enters main (). Similarly, the destructor attribute causes the function to be called automatically after main () has completed or exit () has been called. Functions with these attributes are useful for initializing data that will be used implicitly during the execution of the program.
You may provide an optional integer priority to control the order in which constructor and destructor functions are run. A constructor with a smaller priority number runs before a constructor with a larger priority number; the opposite relationship holds for destructors. So, if you have a constructor that allocates a resource and a destructor that deallocates the same resource, both functions typically have the same priority. The priorities for constructor and destructor functions are the same as those specified for namespace-scope C++ objects (see C++ Attributes).

These attributes are not currently implemented for Objective-C.


   大致意思就是,可以给一个函数赋予constructor或destructor,其中constructor在main开始运行之前被调用,destructor在main函数结束后被调用。如果有多个constructor或destructor,可以给每个constructor或destructor赋予优先级,对于constructor,优先级数值越小,运行越早。destructor则相反。
   下面是一个例子:
#include <stdio.h>

__attribute__((constructor(101))) void foo()
{
    printf("in constructor of foo\n");
}
__attribute__((constructor(102))) void foo1()
{
    printf("in constructor of foo1\n");
}
__attribute__((destructor)) void bar()
{
    printf("in constructor of bar\n");
}

int main()
{
        printf("in main\n");
        return 0;
}

  
  运行的结果为:

in constructor of foo
in constructor of foo1
in main
in constructor of bar