GNU C 中的__attribute__ (二)

来源:互联网 发布:烧香拜佛软件 编辑:程序博客网 时间:2024/06/08 08:14
constructor and destructor:


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.


static void begin(void) __attribute__ ((constructor));static void end(void) __attribute__ ((destructor));int main(int argc, char *argv[]){    printf("begin() = %p\n", begin);    printf("end() = %p\n", end);    exit(0);}void begin(void){    printf("begin!\n");}void end(void){    printf("end!\n");}

outputs:

gcc -o test test.c
./test
begin!
begin() = 0×8048465
end() = 0×8048479
end!



原创粉丝点击