[問題] C 語言的 Bit fields

看板C_and_CPP (C/C++)作者時間9年前 (2016/07/19 09:26), 編輯推噓1(1013)
留言14則, 3人參與, 最新討論串1/1
關於 C 的 Bit fields 有一個段落不太瞭解,想請教各位一下 在 Cppreference 網頁中提到 http://en.cppreference.com/w/c/language/bit_field The following properties of bit fields are implementation-defined: ‧...(省略)... ‧Whether a bit field can straddle an allocation unit boundary 請問上文中提到 "straddle" 與 "allocation unit boundary"是什麼意思? 感謝! -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 42.72.76.81 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1468891573.A.0A2.html

07/19 10:40, , 1F
straddle就是坐在牆壁上,兩腿跨在兩邊。
07/19 10:40, 1F

07/19 10:41, , 2F
allocation unit boundary是記憶體配置(allocation)
07/19 10:41, 2F

07/19 10:42, , 3F
最小單位(unit)的邊界(boundary) 你是不是沒有字典
07/19 10:42, 3F

07/19 10:43, , 4F
簡而言之這個單位在32-bit系統上是4 bytes
07/19 10:43, 4F

07/19 10:45, , 5F
而某個field如果跨4 bytes的邊界 例如剛好在bit 28-35
07/19 10:45, 5F

07/19 10:45, , 6F
那要不要給他跨 還是讓他擠到下一個4 bytes去
07/19 10:45, 6F

07/19 10:46, , 7F
整包就會多出4 bytes 要看compiler怎麼做 標準不規定
07/19 10:46, 7F

07/19 12:54, , 8F
有查字典,只是全拼在一起就看不懂了。謝謝解答!
07/19 12:54, 8F

07/19 15:35, , 9F
組語或計算機組織應該都會提到這個最小單位 可以留意
07/19 15:35, 9F

07/19 19:09, , 10F
記憶體對齊
07/19 19:09, 10F

07/20 18:13, , 11F
寫錯了 這個狀況應該不管怎麼樣也會多出4 bytes
07/20 18:13, 11F

07/20 18:15, , 12F
只是他要使用bits 28-35 或者推到32-39 28-31讓他空著
07/20 18:15, 12F

07/20 18:16, , 13F
則由compiler來決定
07/20 18:16, 13F

07/20 20:45, , 14F
我也沒注意到4bytes的部份XD 不過可以理解的意思
07/20 20:45, 14F
文章代碼(AID): #1NZO6r2Y (C_and_CPP)
文章代碼(AID): #1NZO6r2Y (C_and_CPP)