www.久久久久|狼友网站av天堂|精品国产无码a片|一级av色欲av|91在线播放视频|亚洲无码主播在线|国产精品草久在线|明星AV网站在线|污污内射久久一区|婷婷综合视频网站

當(dāng)前位置:首頁 > 芯聞號(hào) > 充電吧
[導(dǎo)讀]__attribute__:?GNU C 的一大特色就是__attribute__ 機(jī)制。__attribute__ 可以設(shè)置函數(shù)屬性(Function),變量屬性(Variable)和類型屬性(Ty


__attribute__:?GNU C 的一大特色就是__attribute__ 機(jī)制。__attribute__ 可以設(shè)置函數(shù)屬性(Function),變量屬性(Variable)和類型屬性(Type)。

__attribute__ 書寫特征是:

---- __attribute__ 前后都有兩個(gè)下劃線,并切后面會(huì)緊跟一對(duì)原括弧,括弧里面是相應(yīng)的__attribute__ 參數(shù)。

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

__attribute__關(guān)鍵字主要是用來在函數(shù)或數(shù)據(jù)聲明中設(shè)置其屬性。給函數(shù)設(shè)置屬性的主要目的在于讓編譯器進(jìn)行優(yōu)化。GCC為函數(shù)提供了幾種類型的屬性,其中包含:

1)構(gòu)造函數(shù)(constructors)和析構(gòu)函數(shù)(destructors)。

__attribute__((constructor)): 需要在main函數(shù)之前調(diào)用的函數(shù),可以設(shè)置constructor屬性,實(shí)現(xiàn)初始化。

2)packed屬性:使用該屬性可以使得變量或者結(jié)構(gòu)體成員使用最小的對(duì)齊方式。

__attribute__((packed))的作用就是告訴編譯器取消結(jié)構(gòu)在編譯過程中的優(yōu)化對(duì)齊,按照實(shí)際占用字節(jié)數(shù)進(jìn)行對(duì)齊,是GCC特有的語法。這個(gè)功能和OS沒有關(guān)系,和編譯器有關(guān),gcc的編譯器不是緊湊模式的,在windows下,vc的編譯器也不是緊湊的,tc的編譯器是緊湊的。例如:

在GCC下:?

struct?data{
	char?ch;?
	int?num;
};

sizeof(int) = 4; sizeof(data) = 8; (非緊湊模式)

struct?data{
	char?ch;?
	int?num;
}__attribute__((packed));

sizeof(int) = 4; sizeof(data) = 5;

程序員應(yīng)當(dāng)使用類似下面的方式來指定這些屬性:


__attribute__((constructor))?static?void?beforeFunction()
{
????printf("beforeFunctionn");
}

查閱GNU的文檔:



constructor ()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() completes?or exit() is called.

--- Functions with these attributes are useful for initializing data that is used implicitly during the execution?of the program.You can set an optional integer priority to control the order in which constructor and destructor funcs?are run. ?A constructorwith a smaller priority?number?runs before a constructor with a larger priority number;the?opposite relationship holds for destructors. 構(gòu)造函數(shù)優(yōu)先級(jí)數(shù)字越小,越先執(zhí)行。析構(gòu)函數(shù)相反。


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.?


/*?max?length?*/
#define?MAX_LEN?16
/*server?max?number?*/
#define?MAX_SNUM?10
/*port?max?number*/
#define?MAX_PNUM?10
enum?server_state{idle?=?0,in_use};

//server?information
typedef?struct?server_info{
????char?host_name[MAX_LEN];
	int?port_id[MAX_PNUM];
	enum?server_state?st[MAX_PNUM];
	char?used_by[MAX_PNUM][MAX_LEN];
	char?ip_address[MAX_PNUM][MAX_LEN];
}?S_Info;

char?pre_host_name[MAX_SNUM][MAX_LEN]?=?{
????????????????????"eslruntime01",
????????????????????"eslruntime02",
????????????????????"eslruntime03",
????????????????????"eslruntime04",
????????????????????"eslruntime05",
????????????????????"eslruntime06",
????????????????????"eslruntime07",
????????????????????"eslruntime08",
????????????????????"eslruntime09",
????????????????????"eslruntime10"
};

int?port_inf[MAX_PNUM]?=?{10000,10001,10002,10003,10004,10005,10006,10007,10008,10009};
enum?server_state?sst[MAX_PNUM]?=?{idle};
//create?server?table
static?S_Info?*?init_server_info()
{
????S_Info?*?server_t?=?(S_Info?*)malloc(sizeof(S_Info)?*?MAX_SNUM);
????int?i,j,k;
????for?(i?=?0;i?<?MAX_SNUM;++i)
	{?
	????for(j?=?0;j?<?MAX_PNUM;++j)
		{
????????????server_t[i].port_id[j]?=?port_inf[j];
????????????server_t[i].st[j]?=?sst[j];
			memset(server_t[i].used_by[j],0,MAX_LEN);
			memset(server_t[i].ip_address[j],0,MAX_LEN);
		}
	????for(k?=?0;k?<?MAX_LEN;++k)
		{
	????????server_t[i].host_name[k]?=?pre_host_name[i][k];		????
	????}			
	}
????return?server_t;
}

//global?variable
S_Info?*?server_total;
static?void?create_total_server()?__attribute__((constructor));
static?void?free_total_server()?__attribute__((destructor));

static?void?create_total_server()
{
	server_total?=?init_server_info();
}

static?void?free_total_server()
{
	free(server_total);
	server_total?=?NULL;
}


可以給屬性設(shè)置優(yōu)先級(jí):



static??__attribute__((constructor(101)))?void?before1()
{

????printf("before1n");
}
static??__attribute__((constructor(102)))?void?before2()
{

????printf("before2n");
}
static??__attribute__((constructor(103)))?void?before3()
{

????printf("before3n");
}


以上三個(gè)函數(shù)會(huì)依照優(yōu)先級(jí)的順序調(diào)用.括號(hào)內(nèi)的數(shù)值(101,102,103)代表優(yōu)先級(jí),另外,我以前看過,這個(gè)1-100的范圍是保留的.

所以,最好從100之后開始用.(但是實(shí)際上,我在項(xiàng)目中測(cè)試100以內(nèi)的,也沒有得到警告)

main函數(shù)之前的,即constructor的優(yōu)先級(jí),數(shù)值越小,越先調(diào)用。destructor中的數(shù)值越大,越先調(diào)用。

關(guān)于格式 --?按照文檔中的說法,__attribute__應(yīng)該放在函數(shù)聲明的尾部;之前.


__attribute__((constructor))?//?在main函數(shù)被調(diào)用之前調(diào)用
__attribute__((destructor))?//?在main函數(shù)被調(diào)用之后調(diào)
#include__attribute__((constructor))?void?before_main()?{?
???printf("before?mainn");?
}?

__attribute__((destructor))?void?after_main()?{?
???printf("after?mainn");?
}?

int?main(int?argc,?char?**argv)?{?
???printf("in?mainn");?
???return?0;?
}
//這個(gè)例子的輸出結(jié)果將會(huì)是:
before?main
in?main
after?main


本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除。
換一批
延伸閱讀

9月2日消息,不造車的華為或?qū)⒋呱龈蟮莫?dú)角獸公司,隨著阿維塔和賽力斯的入局,華為引望愈發(fā)顯得引人矚目。

關(guān)鍵字: 阿維塔 塞力斯 華為

倫敦2024年8月29日 /美通社/ -- 英國(guó)汽車技術(shù)公司SODA.Auto推出其旗艦產(chǎn)品SODA V,這是全球首款涵蓋汽車工程師從創(chuàng)意到認(rèn)證的所有需求的工具,可用于創(chuàng)建軟件定義汽車。 SODA V工具的開發(fā)耗時(shí)1.5...

關(guān)鍵字: 汽車 人工智能 智能驅(qū)動(dòng) BSP

北京2024年8月28日 /美通社/ -- 越來越多用戶希望企業(yè)業(yè)務(wù)能7×24不間斷運(yùn)行,同時(shí)企業(yè)卻面臨越來越多業(yè)務(wù)中斷的風(fēng)險(xiǎn),如企業(yè)系統(tǒng)復(fù)雜性的增加,頻繁的功能更新和發(fā)布等。如何確保業(yè)務(wù)連續(xù)性,提升韌性,成...

關(guān)鍵字: 亞馬遜 解密 控制平面 BSP

8月30日消息,據(jù)媒體報(bào)道,騰訊和網(wǎng)易近期正在縮減他們對(duì)日本游戲市場(chǎng)的投資。

關(guān)鍵字: 騰訊 編碼器 CPU

8月28日消息,今天上午,2024中國(guó)國(guó)際大數(shù)據(jù)產(chǎn)業(yè)博覽會(huì)開幕式在貴陽舉行,華為董事、質(zhì)量流程IT總裁陶景文發(fā)表了演講。

關(guān)鍵字: 華為 12nm EDA 半導(dǎo)體

8月28日消息,在2024中國(guó)國(guó)際大數(shù)據(jù)產(chǎn)業(yè)博覽會(huì)上,華為常務(wù)董事、華為云CEO張平安發(fā)表演講稱,數(shù)字世界的話語權(quán)最終是由生態(tài)的繁榮決定的。

關(guān)鍵字: 華為 12nm 手機(jī) 衛(wèi)星通信

要點(diǎn): 有效應(yīng)對(duì)環(huán)境變化,經(jīng)營(yíng)業(yè)績(jī)穩(wěn)中有升 落實(shí)提質(zhì)增效舉措,毛利潤(rùn)率延續(xù)升勢(shì) 戰(zhàn)略布局成效顯著,戰(zhàn)新業(yè)務(wù)引領(lǐng)增長(zhǎng) 以科技創(chuàng)新為引領(lǐng),提升企業(yè)核心競(jìng)爭(zhēng)力 堅(jiān)持高質(zhì)量發(fā)展策略,塑強(qiáng)核心競(jìng)爭(zhēng)優(yōu)勢(shì)...

關(guān)鍵字: 通信 BSP 電信運(yùn)營(yíng)商 數(shù)字經(jīng)濟(jì)

北京2024年8月27日 /美通社/ -- 8月21日,由中央廣播電視總臺(tái)與中國(guó)電影電視技術(shù)學(xué)會(huì)聯(lián)合牽頭組建的NVI技術(shù)創(chuàng)新聯(lián)盟在BIRTV2024超高清全產(chǎn)業(yè)鏈發(fā)展研討會(huì)上宣布正式成立。 活動(dòng)現(xiàn)場(chǎng) NVI技術(shù)創(chuàng)新聯(lián)...

關(guān)鍵字: VI 傳輸協(xié)議 音頻 BSP

北京2024年8月27日 /美通社/ -- 在8月23日舉辦的2024年長(zhǎng)三角生態(tài)綠色一體化發(fā)展示范區(qū)聯(lián)合招商會(huì)上,軟通動(dòng)力信息技術(shù)(集團(tuán))股份有限公司(以下簡(jiǎn)稱"軟通動(dòng)力")與長(zhǎng)三角投資(上海)有限...

關(guān)鍵字: BSP 信息技術(shù)
關(guān)閉
關(guān)閉