zoukankan      html  css  js  c++  java
  • C語言中資料結構(struct)的大小

    通常在PC上寫程式時,很少會去管struct會佔掉多少記憶體。
    當要使用到時,也不會想去用手算到底佔掉多少,大多是直接使用sizeof來做計算。
    然而sizeof計算出來的值往往不會如我們想的一樣。因為compiler為了效能考量,會自動地為我們
    做最佳化,也就是資料對齊。為了這個目的,compiler會為struct多準備一些記憶體。
    我們可以看以下的code:
    struct ABC {
    int index;
    char name[6];
    int score;
    };

    struct DEF{
    int att;
    char name[3];
    };

    int main(void)
    {
    printf("sizeof(ABC) = %d ", sizeof(struct ABC));
    printf("sizeof(DEF) = %d ", sizeof(struct DEF));
    return 0;

    }
    說明:
    1. 若我們直接去計算struct ABC和strcut DEF時,
    struct ABC = 4 + 6 + 4 = 14 (struct ABC用掉14個byte)
    strcut DEF = 4 + 3 = 7 (struct DEF用掉7個byte)
    2. 但真的是這樣嗎?程式執行出來的結果卻是,
    sizeof(ABC) = 16
    sizeof(DEF) = 8
    3. 這就是compiler為我們做了對齊的最佳化,將這二個的struct都調整成2的次方。
    這樣有利於運算。

    這樣的做法在PC上通常沒有問題,但若是在嵌入式系統上,記憶體必需要錙珠必較時
    ,我們就必須要考量到使用struct所佔掉的記憶體空間,上次和Tick討論Linux kernel
    裡的List結構時,遇到了這個問題。他告訴我可以使用__attribute__((packed));這個關鍵字,
    它的作用在於叫compiler不要為我們做對齊的最佳化,因此,計算的結果就會如同我們所想的一樣了。
    struct ABC {
    int index;
    char name[6];
    int score;
    } __attribute__((packed));;

    struct DEF{
    int att;
    char name[3];
    } __attribute__((packed));;

    int main(void)
    {
    printf("sizeof(ABC) = %d ", sizeof(struct ABC));
    printf("sizeof(DEF) = %d ", sizeof(struct DEF));
    return 0;

    }
    這樣就會得到以下的結果了。
    sizeof(ABC) = 14
    sizeof(DEF) = 7

    這裡沒有哪一種用法比較好的問題,端看在使用上的需求,
    要運算速度快,就需要資料對齊。要節省記憶體的使用,就取消對齊。

  • 相关阅读:
    闭包 (Closure)
    RSA算法
    HTTPS
    SSH
    HDU1754 I hate it_线段树(入门级别)
    HDU1166 敌兵布阵_线段树
    c++运算符优先级表
    归并排序练习.
    HDU 1969 精度二分
    uva10944 状态压缩bfs or DP
  • 原文地址:https://www.cnblogs.com/bittorrent/p/3258890.html
Copyright © 2011-2022 走看看