20110718

what's __attribute__((packed))

通常在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\n", sizeof(struct ABC));

printf("sizeof(DEF) = %d\n", 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所佔掉的記憶體空間,可以使用__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\n", sizeof(struct ABC));

printf("sizeof(DEF) = %d\n", sizeof(struct DEF));

return 0;

}

這樣就會得到以下的結果了。

sizeof(ABC) = 14

sizeof(DEF) = 7

這裡沒有哪一種用法比較好的問題,端看在使用上的需求,

要運算速度快,就需要資料對齊。要節省記憶體的使用,就取消對齊。

http://chunchaichang.blogspot.com/2010/06/cstruct-attributepacked.html

----
more
http://bbs.chinaunix.net/thread-1253997-1-2.html
http://huenlil.pixnet.net/blog/post/26078382