[問題] Pointer alignment 問題

看板C_and_CPP作者時間8年前 (2017/03/02 21:16), 8年前編輯推噓10(10037)
留言47則, 12人參與, 最新討論串1/2 (看更多)
開發平台(Platform): (Ex: Win10, Linux, ...) Win10 編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出) VC++ 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 假設我今天有一個class 長這樣: class A{ int a; int b; int c; public: void Init( int x, int y, int z ){ a = x; b = y; c = z; } }; 如果他size是12 byte, 我想做以下事情, 請問是否合法? ( 我知道有palcement new, 但出於某些原因我無法用 ) 撇開效能問題, 這樣執行是否會有問題? 1. char myPool[ 1024 ]; A* pA = ( A* ) ( &myPool[ 0 ] ); pA->Init( 1, 2, 3 ); 2. char myPool[ 1024 ]; A* pA = ( A* ) ( &myPool[ 1 ] ); pA->Init( 1, 2, 3 ); 3.如果要align pA, pA應該要align 4 還是 12? 感謝回答 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 24.92.128.112 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1488460567.A.085.html ※ 編輯: Ninja5566 (24.92.128.112), 03/02/2017 21:16:32

03/02 21:19, , 1F
對class做這種事情好像有點危險?
03/02 21:19, 1F

03/02 21:51, , 2F
3. 不能 align non power of 2 的樣子
03/02 21:51, 2F

03/02 21:56, , 3F
VC++的debug有memory window 進debug看最準
03/02 21:56, 3F

03/02 22:02, , 4F
align 12
03/02 22:02, 4F

03/02 22:03, , 5F
我有跑過並且可執行, 但我不知道這樣做是不是合法的
03/02 22:03, 5F

03/02 22:07, , 6F
OK的
03/02 22:07, 6F

03/02 22:15, , 8F
這將來出問題看你怎麼debug XD
03/02 22:15, 8F
請問為什麼不能debug?

03/02 22:15, , 9F
alignment is an integer value always a power of 2
03/02 22:15, 9F

03/02 22:15, , 10F
我應該沒記錯吧
03/02 22:15, 10F
那請問array of A會對齊多少? ※ 編輯: Ninja5566 (24.92.128.112), 03/02/2017 22:18:07

03/02 22:24, , 11F
A[10],每個都對齊12
03/02 22:24, 11F

03/02 22:28, , 12F
alignment會是std::size_t的倍數,power of 2應該是形容
03/02 22:28, 12F

03/02 22:28, , 13F
std::size_t的大小
03/02 22:28, 13F

03/02 23:21, , 14F
為何不宣告A陣列
03/02 23:21, 14F

03/02 23:31, , 15F
因為我要作 memory pool
03/02 23:31, 15F

03/02 23:36, , 16F
借問,用placement new難道就不用考慮alighnment嗎?
03/02 23:36, 16F

03/03 00:03, , 17F
placement new 只是把"要空間"跟"初始化"兩件事分開而已
03/03 00:03, 17F

03/03 00:03, , 18F
alignment 是"要空間"部份旳問題, 跟初始化無關
03/03 00:03, 18F
所以C++ 保證只要空間足夠, 就可以直接對pointer 初始化操作嗎? 還是必須要standard layout?

03/03 00:04, , 19F
然後事實上 pA->Init() 跟 placement new 其實有 87% 像
03/03 00:04, 19F

03/03 00:06, , 20F
最後, 問於一樓的問題, 如果 class 是 standard layout
03/03 00:06, 20F

03/03 00:07, , 21F
那各元素的位置是確定的, 這時可以做一些手腳去達到對齊
03/03 00:07, 21F
可以做一些手腳達到對齊 ->請問這句話是甚麼意思?

03/03 00:07, , 22F
不是 standard layout 基本上就不行了
03/03 00:07, 22F

03/03 00:10, , 23F
standard layout 是 POD的某一個條件嗎?
03/03 00:10, 23F

03/03 00:11, , 24F
倒過來, POD 是 standard layout 的一種
03/03 00:11, 24F

03/03 00:12, , 25F
但 standard layout 可以包含一些有限度的繼承
03/03 00:12, 25F

03/03 00:17, , 27F
這樣寫 不是說standardlayout是POD的一種嗎?
03/03 00:17, 27F

03/03 00:20, , 28F
http://ideone.com/sWh0je 測一下 有繼承也是POD?
03/03 00:20, 28F

03/03 00:20, , 29F
喔, 那裡框框中那三行是 and 的關係, 不是 or 的
03/03 00:20, 29F
※ 編輯: Ninja5566 (140.254.77.191), 03/03/2017 00:22:28 ※ 編輯: Ninja5566 (140.254.77.191), 03/03/2017 00:23:52 ※ 編輯: Ninja5566 (140.254.77.191), 03/03/2017 00:27:37

03/03 00:29, , 30F
placement new 是對任何 C++ 物件型態都能進行的操作
03/03 00:29, 30F

03/03 00:30, , 31F
它就只是把你給的指標當做物件未來的所在地進行初始化而已
03/03 00:30, 31F

03/03 00:32, , 32F
那standard layout的條件可以做到額外甚麼事情?
03/03 00:32, 32F

03/03 00:32, , 33F
所以我才會說這跟你的 pA->Init() 有 87% 像
03/03 00:32, 33F

03/03 00:33, , 34F
standard layout 最大的好處是成員位置固定
03/03 00:33, 34F

03/03 00:33, , 35F
因此可以拿來當做一些二進位資料交換的用途
03/03 00:33, 35F

03/03 00:34, , 36F
那也因為位置固定, 所以只要知道想要對齊的成員是怎麼排的
03/03 00:34, 36F

03/03 00:34, , 37F
像是serialization?
03/03 00:34, 37F

03/03 00:34, , 38F
就能調整開始位置使得其成員處在對齊的位置上
03/03 00:34, 38F
※ 編輯: Ninja5566 (140.254.77.191), 03/03/2017 00:34:59

03/03 00:35, , 39F
serialization 是另一回事了, 這裡指的是整包寫出去那種
03/03 00:35, 39F

03/03 00:35, , 40F
例如二進位檔案格式那樣
03/03 00:35, 40F

03/03 00:45, , 41F
所以standard layout 保證成員變數的memory layout?
03/03 00:45, 41F

03/03 00:56, , 42F
對, 這也是為什麼 offsetof() 可以用在 standard layout 上
03/03 00:56, 42F

03/03 00:57, , 43F
(或者該說「只能用在 standard layout 上」)
03/03 00:57, 43F

03/03 01:02, , 44F
感謝解答!!
03/03 01:02, 44F
但是在這連結中卻說這是 undefined behavior? https://tinyurl.com/j4atmr8 [14] Note: when the allocation function returns a value other than null, it must be a pointer to a block of storage in which space for the object has been reserved. The block of storage is assumed to be appropriately aligned and of the requested size. 請問這裡講的是對的嗎? 又. 我測試發現 std::alignment< A >::value = 4 所以只要對齊4就好? https://tinyurl.com/pqkgqnh Inorder to avoid such misalignment, compiler will introduce alignment requirement to every structure. It will be as that of the largest member of the structure. In our case alignment of structa_t is 2, structb_t is 4 and structc_t is 8. If we need nested structures, the size of largest inner structure will be the alignment of immediate larger structure. ※ 編輯: Ninja5566 (24.92.128.112), 03/03/2017 03:41:43

03/03 10:03, , 45F
請用struct與offsetof
03/03 10:03, 45F
※ 編輯: Ninja5566 (24.92.128.112), 03/03/2017 21:00:05 ※ 編輯: Ninja5566 (24.92.128.112), 03/03/2017 21:10:51

03/07 22:08, , 46F
沒人跟我一樣覺得"副程式"這個稱呼很classic嗎
03/07 22:08, 46F

03/07 22:09, , 47F
不小心耍腦推錯篇= =
03/07 22:09, 47F
文章代碼(AID): #1Ok1iN25 (C_and_CPP)
文章代碼(AID): #1Ok1iN25 (C_and_CPP)