全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:876
推到 Plurk!
推到 Facebook!

用 sizeof 取得 struct 的大小的困惑?

尚未結案
forth
一般會員


發表:13
回覆:34
積分:9
註冊:2002-10-30

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-06-22 15:38:49 IP:140.115.xxx.xxx 未訂閱
typedef struct { unsigned int a; unsigned int b; unsigned short c; unsigned short d; unsigned short e; unsigned short f; } X; typedef struct { unsigned int a; unsigned int b; unsigned short c; unsigned short d; unsigned short e; } Y; int a = sizeof(X); int b = sizeof(Y); int c = sizeof(unsigned int); int d = sizeof(unsigned short); 以上所得 a= 16, b= 16, c=4, b=2; 為什麼 a and b 會是一樣的結果, 我在 VC and DEV-C RUN 的結果都是一樣的, 難道是 sizeof 用在 struct 所得的結果要4的倍數嗎? 還是他是一個 bug 不知有誰知道? 謝! 寫程式是建設,除錯是破壞. 寫程式是樂趣,除錯是苦難. 哈! 哈! ....
supman
尊榮會員


發表:29
回覆:770
積分:924
註冊:2002-04-22

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-06-22 16:15:16 IP:219.68.xxx.xxx 未訂閱
您好: 您的問題跟下面這篇一樣,請參考一下。 http://delphi.ktop.com.tw/topic.php?topic_id=70130
GeorgeKu
中階會員


發表:1
回覆:120
積分:74
註冊:2004-10-13

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-06-22 16:19:23 IP:61.228.xxx.xxx 未訂閱
你可以在前端加個#pragma pack(1),在跑跑看有何不同,這是位元組對齊的問題,你可以參考下列的文章http://delphi.ktop.com.tw/topic.php?topic_id=64379
系統時間:2024-08-28 2:53:06
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!