一行代碼勝過千言萬語:)這裏是我的問題:C++內存對齊問題
/* Platform specific 16-byte alignment macro switch.
On Visual C++ it would substitute __declspec(align(16)).
On GCC it substitutes __attribute__((aligned (16))).
*/
#define ALIGN_16 ...
struct ALIGN_16 A {...};
A* ptr = new A;
A* ptr2 = new A[20];
assert(size_t(ptr) % 16 == 0);
for (int i=0; i<20; ++i)
assert(size_t(ptr2+i) % 16 == 0);
assert(sizeof(A) % 16 == 0);
我能想到的所有斷言通過與SSE支持的平臺?謝謝。
編輯。部分答案。我用VS2008,GCC和ICC做了一些測試。 MS編譯器確實對齊ptr和ptr2,但是GCC和ICC未能對齊ptr2。
爲什麼20的陣列如? – John 2010-12-15 00:03:08
只是我的頭。如果陣列中的每個元素都對齊,我就會徘徊。 – watson1180 2010-12-15 00:22:49
正確分配的數組的元素由標準保證爲所討論的類型正確對齊。然而,這種對齊是一個實現細節,理論上可能是一個字節(即壓縮對齊)。 – 2010-12-15 00:30:01