[問題] Pointer alignment 問題
開發平台(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
03/02 21:19, 1F
→
03/02 21:51, , 2F
03/02 21:51, 2F
推
03/02 21:56, , 3F
03/02 21:56, 3F
→
03/02 22:02, , 4F
03/02 22:02, 4F
→
03/02 22:03, , 5F
03/02 22:03, 5F
→
03/02 22:07, , 6F
03/02 22:07, 6F
→
03/02 22:14, , 7F
03/02 22:14, 7F
→
03/02 22:15, , 8F
03/02 22:15, 8F
請問為什麼不能debug?
→
03/02 22:15, , 9F
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
03/02 22:24, 11F
→
03/02 22:28, , 12F
03/02 22:28, 12F
→
03/02 22:28, , 13F
03/02 22:28, 13F
→
03/02 23:21, , 14F
03/02 23:21, 14F
→
03/02 23:31, , 15F
03/02 23:31, 15F
推
03/02 23:36, , 16F
03/02 23:36, 16F
推
03/03 00:03, , 17F
03/03 00:03, 17F
→
03/03 00:03, , 18F
03/03 00:03, 18F
所以C++ 保證只要空間足夠, 就可以直接對pointer 初始化操作嗎?
還是必須要standard layout?
→
03/03 00:04, , 19F
03/03 00:04, 19F
→
03/03 00:06, , 20F
03/03 00:06, 20F
→
03/03 00:07, , 21F
03/03 00:07, 21F
可以做一些手腳達到對齊 ->請問這句話是甚麼意思?
→
03/03 00:07, , 22F
03/03 00:07, 22F
推
03/03 00:10, , 23F
03/03 00:10, 23F
→
03/03 00:11, , 24F
03/03 00:11, 24F
→
03/03 00:12, , 25F
03/03 00:12, 25F
推
03/03 00:17, , 26F
03/03 00:17, 26F
→
03/03 00:17, , 27F
03/03 00:17, 27F
→
03/03 00:20, , 28F
03/03 00:20, 28F
→
03/03 00:20, , 29F
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
03/03 00:29, 30F
→
03/03 00:30, , 31F
03/03 00:30, 31F
→
03/03 00:32, , 32F
03/03 00:32, 32F
→
03/03 00:32, , 33F
03/03 00:32, 33F
→
03/03 00:33, , 34F
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
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
03/03 00:35, 39F
→
03/03 00:35, , 40F
03/03 00:35, 40F
→
03/03 00:45, , 41F
03/03 00:45, 41F
推
03/03 00:56, , 42F
03/03 00:56, 42F
→
03/03 00:57, , 43F
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
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
03/07 22:08, 46F
→
03/07 22:09, , 47F
03/07 22:09, 47F
討論串 (同標題文章)
完整討論串 (本文為第 1 之 2 篇):