今天寫程序的時候遇到一個小難題,如何記錄下64個設備的有無動作的表示,我想到了節省資源的位域,寫下如下的程序(32位PC下) [cpp] typedef struct { unsigned f1 : 1; }new_log_t; typedef struct { unsigned f1 : 1; }new_log_t;sizeof(new_log_t) = 4,似乎沒有節省資源嗎,再試 [cpp] typedef struct { unsigned f1 : 1; unsigned f2 : 1; unsigned f3 : 1; unsigned f4 : 1; unsigned f5 : 1; unsigned f6 : 1; unsigned f7 : 1; unsigned f8 : 1; }new_log_t; typedef struct { unsigned f1 : 1; unsigned f2 : 1; unsigned f3 : 1; unsigned f4 : 1; unsigned f5 : 1; unsigned f6 : 1; unsigned f7 : 1; unsigned f8 : 1; }new_log_t;sizeof(new_log_t) = 4,似乎了解什麼了,再試 [cpp] typedef struct { unsigned f1 : 1; unsigned f2 : 1; unsigned f3 : 1; unsigned f4 : 1; unsigned f5 : 1; unsigned f6 : 1; unsigned f7 : 1; unsigned f8 : 1; unsigned f9 : 1; unsigned f10 : 1; unsigned f11 : 1; unsigned f12 : 1; unsigned f13 : 1; unsigned f14 : 1; unsigned f15 : 1; unsigned f16 : 1; unsigned f17 : 1; unsigned f18: 1; unsigned f19 : 1; unsigned f20 : 1; unsigned f21 : 1; unsigned f22 : 1; unsigned f23 : 1; unsigned f24 : 1; unsigned f25 : 1; unsigned f26 : 1; unsigned f27 : 1; unsigned f28 : 1; unsigned f29 : 1; unsigned f30 : 1; unsigned f31 : 1; unsigned f32 : 1; unsigned f33 : 1; }new_log_t; typedef struct { unsigned f1 : 1; unsigned f2 : 1; unsigned f3 : 1; unsigned f4 : 1; unsigned f5 : 1; unsigned f6 : 1; unsigned f7 : 1; unsigned f8 : 1; unsigned f9 : 1; unsigned f10 : 1; unsigned f11 : 1; unsigned f12 : 1; unsigned f13 : 1; unsigned f14 : 1; unsigned f15 : 1; unsigned f16 : 1; unsigned f17 : 1; unsigned f18: 1; unsigned f19 : 1; unsigned f20 : 1; unsigned f21 : 1; unsigned f22 : 1; unsigned f23 : 1; unsigned f24 : 1; unsigned f25 : 1; unsigned f26 : 1; unsigned f27 : 1; unsigned f28 : 1; unsigned f29 : 1; unsigned f30 : 1; unsigned f31 : 1; unsigned f32 : 1; unsigned f33 : 1; }new_log_t;sizeof(new_log_t) = 8 ,看一看只有32個成員時候占多少吧 [cpp] typedef struct { unsigned f1 : 1; unsigned f2 : 1; unsigned f3 : 1; unsigned f4 : 1; unsigned f5 : 1; unsigned f6 : 1; unsigned f7 : 1; unsigned f8 : 1; unsigned f9 : 1; unsigned f10 : 1; unsigned f11 : 1; unsigned f12 : 1; unsigned f13 : 1; unsigned f14 : 1; unsigned f15 : 1; unsigned f16 : 1; unsigned f17 : 1; unsigned f18: 1; unsigned f19 : 1; unsigned f20 : 1; unsigned f21 : 1; unsigned f22 : 1; unsigned f23 : 1; unsigned f24 : 1; unsigned f25 : 1; unsigned f26 : 1; unsigned f27 : 1; unsigned f28 : 1; unsigned f29 : 1; unsigned f30 : 1; unsigned f31 : 1; unsigned f32 : 1; }new_log_t; typedef struct { unsigned f1 : 1; unsigned f2 : 1; unsigned f3 : 1; unsigned f4 : 1; unsigned f5 : 1; unsigned f6 : 1; unsigned f7 : 1; unsigned f8 : 1; unsigned f9 : 1; unsigned f10 : 1; unsigned f11 : 1; unsigned f12 : 1; unsigned f13 : 1; unsigned f14 : 1; unsigned f15 : 1; unsigned f16 : 1; unsigned f17 : 1; unsigned f18: 1; unsigned f19 : 1; unsigned f20 : 1; unsigned f21 : 1; unsigned f22 : 1; unsigned f23 : 1; unsigned f24 : 1; unsigned f25 : 1; unsigned f26 : 1; unsigned f27 : 1; unsigned f28 : 1; unsigned f29 : 1; unsigned f30 : 1; unsigned f31 : 1; unsigned f32 : 1; }new_log_t;sizeof(new_log_t) = 4,我明白了,則 [cpp] typedef struct { unsigned f1 : 1; unsigned f2 : 1; unsigned f3 : 1; unsigned f4 : 1; unsigned f5 : 1; unsigned f6 : 1; unsigned f7 : 1; unsigned f8 : 1; unsigned f9 : 1; unsigned f10 : 1; unsigned f11 : 1; unsigned f12 : 1; unsigned f13 : 1; unsigned f14 : 1; unsigned f15 : 1; unsigned f16 : 1; unsigned f17 : 1; unsigned f18: 1; unsigned f19 : 1; unsigned f20 : 1; unsigned f21 : 1; unsigned f22 : 1; unsigned f23 : 1; unsigned f24 : 1; unsigned f25 : 1; unsigned f26 : 1; unsigned f27 : 1; unsigned f28 : 1; unsigned f29 : 1; unsigned f30 : 1; unsigned f31 : 1; unsigned f32 : 1; unsigned f33 : 1; unsigned f34 : 1; unsigned f35 : 1; unsigned f36 : 1; unsigned f37 : 1; unsigned f38 : 1; unsigned f39 : 1; unsigned f40 : 1; unsigned f41 : 1; unsigned f42 : 1; unsigned f43 : 1; unsigned f44 : 1; unsigned f45 : 1; unsigned f46 : 1; unsigned f47 : 1; unsigned f48 : 1; unsigned f49: 1; unsigned f50 : 1; unsigned f51 : 1; unsigned f52 : 1; unsigned f53 : 1; unsigned f54 : 1; unsigned f55 : 1; unsigned f56 : 1; unsigned f57 : 1; unsigned f58 : 1; unsigned f59 : 1; unsigned f60 : 1; unsigned f61 : 1; unsigned f62 : 1; unsigned f63 : 1; unsigned f64 : 1; }new_log_t; typedef struct { unsigned f1 : 1; unsigned f2 : 1; unsigned f3 : 1; unsigned f4 : 1; unsigned f5 : 1; unsigned f6 : 1; unsigned f7 : 1; unsigned f8 : 1; unsigned f9 : 1; unsigned f10 : 1; unsigned f11 : 1; unsigned f12 : 1; unsigned f13 : 1; unsigned f14 : 1; unsigned f15 : 1; unsigned f16 : 1; unsigned f17 : 1; unsigned f18: 1; unsigned f19 : 1; unsigned f20 : 1; unsigned f21 : 1; unsigned f22 : 1; unsigned f23 : 1; unsigned f24 : 1; unsigned f25 : 1; unsigned f26 : 1; unsigned f27 : 1; unsigned f28 : 1; unsigned f29 : 1; unsigned f30 : 1; unsigned f31 : 1; unsigned f32 : 1; unsigned f33 : 1; unsigned f34 : 1; unsigned f35 : 1; unsigned f36 : 1; unsigned f37 : 1; unsigned f38 : 1; unsigned f39 : 1; unsigned f40 : 1; unsigned f41 : 1; unsigned f42 : 1; unsigned f43 : 1; unsigned f44 : 1; unsigned f45 : 1; unsigned f46 : 1; unsigned f47 : 1; unsigned f48 : 1; unsigned f49: 1; unsigned f50 : 1; unsigned f51 : 1; unsigned f52 : 1; unsigned f53 : 1; unsigned f54 : 1; unsigned f55 : 1; unsigned f56 : 1; unsigned f57 : 1; unsigned f58 : 1; unsigned f59 : 1; unsigned f60 : 1; unsigned f61 : 1; unsigned f62 : 1; unsigned f63 : 1; unsigned f64 : 1; }new_log_t;我猜sizeof後是8個字節,sizeof後果然是 8 -------------------------------------------------------------------------- 我的PC 結構體是4個字節對齊的,那我如何才能讓它1個字節對齊,達到我的要求以節省資源呢,使用如下語句 #pragma pack(push, 1) 結構體{} #pragma pack(pop) 示例 [cpp] #pragma pack(push, 1) //效果開始 typedef struct { unsigned f1 : 1; }new_log_t; #pragma pack(pop) //效果結束(其他的沒有被這個命令包著的結構體(至少一個成員)sizeof後是4) #pragma pack(push, 1) //效果開始 typedef struct { unsigned f1 : 1; }new_log_t; #pragma pack(pop) //效果結束(其他的沒有被這個命令包著的結構體(至少一個成員)sizeof後是4)sizeof(new_log_t) = 1,看來精度只能是1個字節了(以後再看看有沒有使精度更高的寫法) 後記:以前一直聽字節對齊字節對齊,不過聽歸聽,只有自己的的確確遇到了這類問題才能深刻理解,結合者需求去看,去理解,才能加深理解,達到事半功倍的效果