STRUCT 的 問題 |
答題得分者是:brook
|
lgyui
初階會員 發表:21 回覆:29 積分:30 註冊:2003-01-31 發送簡訊給我 |
我有一個STRUCT的問題
當我宣告
#include "stdint.h" struct test {
uint32_t first;
uint16_t second;
}; 所得到的 sizeof(struct test)為什麼是8(理論上應該是6) 後來發現是 compiler 在最佳化時自動補了一個位元
在gcc編譯可以用
struct test {
uint32_t first;
uint16_t second;
} __attribute ((packed));
那在BCB要如何做呢??
|
brook
資深會員 發表:57 回覆:323 積分:371 註冊:2002-07-12 發送簡訊給我 |
|
dllee
站務副站長 發表:321 回覆:2519 積分:1711 註冊:2002-04-15 發送簡訊給我 |
引言: 我有一個STRUCT的問題 當我宣告 #include "stdint.h" struct test { uint32_t first; uint16_t second; }; 所得到的 sizeof(struct test)為什麼是8(理論上應該是6) 後來發現是 compiler 在最佳化時自動補了一個位元 在gcc編譯可以用 struct test { uint32_t first; uint16_t second; } __attribute ((packed)); 那在BCB要如何做呢??本來想跟您說,請您去買「BCB K.Top 程式設計師手札」這本書... 可是還沒出版 在 > src="http://delphi.ktop.com.tw/loadfile.php?TOPICID=9206908&CC=205912"> 沒空更新的網頁... http://dllee.ktop.com.tw C及指標教學,計算機概論,資訊管理導論... http://big5.to/吃軟也吃硬 http://coolsite.to/ushells 介紹Shells,LiteStep,GeoShell....
------
http://www.ViewMove.com |
dllee
站務副站長 發表:321 回覆:2519 積分:1711 註冊:2002-04-15 發送簡訊給我 |
重新貼圖:
沒空更新的網頁...
http://dllee.ktop.com.tw C及指標教學,計算機概論,資訊管理導論... http://big5.to/吃軟也吃硬 http://coolsite.to/ushells 介紹Shells,LiteStep,GeoShell....
------
http://www.ViewMove.com |
harpist
資深會員 發表:3 回覆:251 積分:430 註冊:2002-10-03 發送簡訊給我 |
雖然問題已經年代久遠但還是雞婆一下
在 BCB 可以使用 Preprocessor directive 來達成。 <textarea class="cpp" rows="10" cols="60" name="code">#pragma pack(push,1) //byte alignment struct test { __int32 first; __int16 second; }; #pragma pack(pop) </textarea> ===================引 用 lgyui 文 章=================== 我有一個STRUCT的問題當我宣告 #include "stdint.h" struct test { uint32_t first; uint16_t second; }; 所得到的 sizeof(struct test)為什麼是8(理論上應該是6) 後來發現是 compiler 在最佳化時自動補了一個位元在gcc編譯可以用 struct test { uint32_t first; uint16_t second; } __attribute ((packed)); 那在BCB要如何做呢??
------
~§~迷時師渡,悟了自渡~§~
編輯記錄
harpist 重新編輯於 2007-06-09 01:36:13, 註解 無‧
|
dllee
站務副站長 發表:321 回覆:2519 積分:1711 註冊:2002-04-15 發送簡訊給我 |
請問一下,這樣是否表示在同一個程式中,對於不同的 Structure 可以使用不同的 alignment 方式呢?
又如果這是一個 dll 或 lib 檔,那引用的程式也只需 include 的 structure 設定對應的不同 alignment 方式就可以了嗎? ===================引 用 harpist 文 章=================== 雖然問題已經年代久遠但還是雞婆一下 在 BCB 可以使用 Preprocessor directive 來達成。 <textarea name="code" cols="60" rows="10" class="cpp">#pragma pack(push,1) //byte alignment struct test { __int32 first; __int16 second; }; #pragma pack(pop) </textarea>
------
http://www.ViewMove.com |
harpist
資深會員 發表:3 回覆:251 積分:430 註冊:2002-10-03 發送簡訊給我 |
<textarea class="cpp" rows="10" cols="60" name="code">#pragma pack(push,1) //byte alignment
struct test1
{
__int32 first;
__int16 second;
};
#pragma pack(pop) //恢復原來設定
#pragma pack(push,2) //2byte alignment
struct test2
{
__int32 first;
__int16 second;
};
#pragma pack(pop) //恢復原來設定
</textarea>
不同的 Structure 可以使用不同的 data alignment 。 data alignment 編譯前就設好,編譯後要修改應該還是要重新編譯 dll。 ===================引 用 dllee 文 章=================== 請問一下,這樣是否表示在同一個程式中,對於不同的 Structure 可以使用不同的 alignment 方式呢? 又如果這是一個 dll 或 lib 檔,那引用的程式也只需 include 的 structure 設定對應的不同 alignment 方式就可以了嗎?
------
~§~迷時師渡,悟了自渡~§~
編輯記錄
harpist 重新編輯於 2007-06-11 22:17:28, 註解 無‧
|
dllee
站務副站長 發表:321 回覆:2519 積分:1711 註冊:2002-04-15 發送簡訊給我 |
|
暗黑破壞神
版主 發表:9 回覆:2301 積分:1627 註冊:2004-10-04 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |