日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

编译 / __attribute__(constructor)和__attribute__(destructor)

發布時間:2024/10/14 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 编译 / __attribute__(constructor)和__attribute__(destructor) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、前言

最近看代碼,看到一個函數前面用 __attribute__((constructor)) 修飾,搜了整個程序,沒發現哪個地方調用這個函數。如下:

__attribute__((constructor)) void load_file() {printf("Constructor is called.\n");g_count = (int *)malloc(sizeof(int)); }

二、__attribute__ 介紹

__attribute__ 可以設置函數屬性(Function Attribute)、變量屬性(Variable Attribute)和類型屬性(Type Attribute)。

__attribute__ 前后都有兩個下劃線,并且后面會緊跟一對原括弧,括弧里面是相應的__attribute__參數。

__attribute__語法格式為:attribute ( ( attribute-list ) )

如果函數被設定為 constructor 屬性,則該函數會在 main() 函數執行之前被自動的執行;

若函數被設定為 destructor 屬性,則該函數會在 main() 函數執行之后或者 exit() 被調用后被自動的執行。

三、驗證demo程序

例如下面的程序:

#include <stdio.h> #include <stdlib.h> static int * g_count = NULL; __attribute__((constructor)) void load_file() {printf("Constructor is called.\n"); } __attribute__((destructor)) void unload_file() {printf("destructor is called.\n"); }int main() {printf ("this is main function\n");return 0; }

執行結果如下:

Constructor is called.

this is main function

destructor is called.

轉載:__attribute__(constructor)和__attribute__(destructor)_sun172270102的博客-CSDN博客___attribute__(constructor)

(SAW:Game Over!)

總結

以上是生活随笔為你收集整理的编译 / __attribute__(constructor)和__attribute__(destructor)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。