亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

__attribute __((構造函數))究竟是如何工作的?

__attribute __((構造函數))究竟是如何工作的?

C++ C
LEATH 2019-08-14 17:05:48
__attribute __((構造函數))究竟是如何工作的?似乎很清楚它應該設置。什么時候它運行?為什么有兩個括號?是__attribute__功能嗎?一個宏?句法?這在C中有用嗎?C ++?它的工作功能是否需要是靜態的?什么時候__attribute__((destructor))跑?Objective-C中的示例:__attribute__((constructor))static void initialize_navigationBarImages() {   navigationBarImages = [[NSMutableDictionary alloc] init];}__attribute__((destructor))static void destroy_navigationBarImages() {   [navigationBarImages release];}
查看完整描述

3 回答

?
慕斯709654

TA貢獻1840條經驗 獲得超5個贊

本頁提供有關重大認識constructordestructor屬性的實施和內ELF內,讓他們工作的章節。在消化了這里提供的信息之后,我編譯了一些額外的信息,并(借用上面Michael Ambrus的部分示例)創建了一個示例來說明概念并幫助我學習。下面提供這些結果以及示例源。

如此線程中所述,constructordestructor屬性在目標文件的.ctors.dtors部分中創建條目。您可以使用以下三種方式之一在任一部分中放置對函數的引用。(1)使用任何一個section屬性; (2)constructordestructor屬性或(3)內聯匯編調用(在Ambrus的答案中引用了鏈接)。

使用constructordestructor屬性允許您另外為構造函數/析構函數分配優先級,以在main()調用之前或返回之后控制其執行順序。給定的優先級值越低,執行優先級越高(在main()之前的較高優先級之前執行的優先級較低 - 以及在main()之后的較高優先級之后執行)。您提供的優先級值必須大于100編譯器保留0-100之間的優先級值才能實現。A constructordestructor具有優先級的指定在沒有優先級的情況下執行constructordestructor指定。

隨著“部分的”屬性或者與內聯匯編,你也可以將在函數引用.init.finiELF代碼段,將任何構造的任何析構函數之前和之后,分別執行。放置在該.init部分中的函數引用調用的任何函數將在函數引用之前執行(像往常一樣)。

我試圖在下面的例子中說明每一個:

#include <stdio.h>#include <stdlib.h>/*  test function utilizing attribute 'section' ".ctors"/".dtors"
    to create constuctors/destructors without assigned priority.
    (provided by Michael Ambrus in earlier answer)
*/#define SECTION( S ) __attribute__ ((section ( S )))void test (void) {printf("\n\ttest() utilizing -- (.section .ctors/.dtors) w/o priority\n");}void (*funcptr1)(void) SECTION(".ctors") =test;void (*funcptr2)(void) SECTION(".ctors") =test;void (*funcptr3)(void) SECTION(".dtors") =test;/*  functions constructX, destructX use attributes 'constructor' and
    'destructor' to create prioritized entries in the .ctors, .dtors
    ELF sections, respectively.

    NOTE: priorities 0-100 are reserved
*/void construct1 () __attribute__ ((constructor (101)));void construct2 () __attribute__ ((constructor (102)));void destruct1 () __attribute__ ((destructor (101)));void destruct2 () __attribute__ ((destructor (102)));/*  init_some_function() - called by elf_init()
*/int init_some_function () {
    printf ("\n  init_some_function() called by elf_init()\n");
    return 1;}/*  elf_init uses inline-assembly to place itself in the ELF .init section.
*/int elf_init (void){
    __asm__ (".section .init \n call elf_init \n .section .text\n");

    if(!init_some_function ())
    {
        exit (1);
    }

    printf ("\n    elf_init() -- (.section .init)\n");

    return 1;}/*
    function definitions for constructX and destructX
*/void construct1 () {
    printf ("\n      construct1() constructor -- (.section .ctors) priority 101\n");}void construct2 () {
    printf ("\n      construct2() constructor -- (.section .ctors) priority 102\n");}void destruct1 () {
    printf ("\n      destruct1() destructor -- (.section .dtors) priority 101\n\n");}void destruct2 () {
    printf ("\n      destruct2() destructor -- (.section .dtors) priority 102\n");}/* main makes no function call to any of the functions declared above
*/intmain (int argc, char *argv[]) {

    printf ("\n\t  [ main body of program ]\n");

    return 0;}

輸出:

init_some_function() called by elf_init()

    elf_init() -- (.section .init)

    construct1() constructor -- (.section .ctors) priority 101

    construct2() constructor -- (.section .ctors) priority 102

        test() utilizing -- (.section .ctors/.dtors) w/o priority

        test() utilizing -- (.section .ctors/.dtors) w/o priority        [ main body of program ]

        test() utilizing -- (.section .ctors/.dtors) w/o priority

    destruct2() destructor -- (.section .dtors) priority 102

    destruct1() destructor -- (.section .dtors) priority 101

該示例有助于鞏固構造函數/析構函數的行為,希望它對其他人也有用。


查看完整回答
反對 回復 2019-08-14
  • 3 回答
  • 0 關注
  • 1105 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號