線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:2504
推到 Plurk!
推到 Facebook!

STRUCT 的 問題

答題得分者是:brook
lgyui
初階會員


發表:21
回覆:29
積分:30
註冊:2003-01-31

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-04-25 10:19:33 IP:210.66.xxx.xxx 未訂閱
我有一個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

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-04-25 12:47:06 IP:211.76.xxx.xxx 未訂閱
8沒錯,因為最小的單位為4; 要6也可以.project->options->advanced compiler->data alignment->選byte即可.
dllee
站務副站長


發表:321
回覆:2519
積分:1711
註冊:2002-04-15

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-04-25 12:55:05 IP:61.231.xxx.xxx 訂閱
引言: 我有一個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

發送簡訊給我
#4 引用回覆 回覆 發表時間:2003-04-25 13:04:42 IP:61.231.xxx.xxx 訂閱
重新貼圖: 沒空更新的網頁... 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

發送簡訊給我
#5 引用回覆 回覆 發表時間:2007-06-09 01:32:34 IP:59.104.xxx.xxx 未訂閱
雖然問題已經年代久遠但還是雞婆一下
在 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

發送簡訊給我
#6 引用回覆 回覆 發表時間:2007-06-11 21:46:11 IP:59.105.xxx.xxx 訂閱
請問一下,這樣是否表示在同一個程式中,對於不同的 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

發送簡訊給我
#7 引用回覆 回覆 發表時間:2007-06-11 22:16:50 IP:211.74.xxx.xxx 未訂閱
<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

發送簡訊給我
#8 引用回覆 回覆 發表時間:2007-06-11 22:40:01 IP:59.105.xxx.xxx 訂閱
感謝 harpist 的分享,真是太棒了
------
http://www.ViewMove.com
暗黑破壞神
版主


發表:9
回覆:2301
積分:1627
註冊:2004-10-04

發送簡訊給我
#9 引用回覆 回覆 發表時間:2007-06-11 23:19:13 IP:125.231.xxx.xxx 未訂閱
不過,我不會這樣做。我知道可以這樣做。不過,我個人不會這樣搞 BCB.
理由兩個,1.將來要把這樣的程式PO到其它編譯器時,我不用傷腦筋一次。
2.我實在不相信 borland 的技術可以這樣做而不成問題(多個不同的結構)。
還是保守一點,替M$的OS擦屁股已經很不爽了。要是問題又出在使用工具,我會更不爽。
所以我一向都只用標準功能。這個是大家都會用到的,而不是特異功能。
免得沈船了,來KTOP求救時,沒人玩過。那不就糗大了。^___^
發表一下,我的看法。
系統時間:2024-04-17 6:46:09
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!