C ++的内存对齐问题内存、问题

2023-09-11 07:44:33 作者:(り 友情大于天

A线的code是胜过千言万语:)这是我的问题:

A line of code is worth a thousand words :) Here is my problem:

/* 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);

我能想到的所有断言通过与上证所的支持平台?谢谢你。

Can I expect that all assertions pass on platforms with SSE support? Thank you.

编辑。部分答案。我做了一些测试与VS2008,GCC和ICC。 MS编译器做了对齐两个 PTR 和 PTR2 ,但GCC和ICC没有对齐的 PTR2

EDIT. Partial answer. I did some test with VS2008, GCC and ICC. MS compiler did align both ptr and ptr2, but GCC and ICC failed to align ptr2.

推荐答案

Is有地址的回报对准由C ++的新的操作任何保证?

在换句话说,你可以使用标准来证明你的假设,它应该工作,但在实践中,它可能炸毁你的脸。

In other words, you can use the standard to justify your assumption that it should work, but in practice, it may blow up in your face.

的Visual C ++ 6通过未对齐双打分配正常,所以你去。

Visual C++ 6 did not align doubles allocated via new properly, so there you go.

 
精彩推荐
图片推荐