C ++的内存对齐问题 [英] C++ memory alignment question

查看:131
本文介绍了C ++的内存对齐问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

推荐答案

<一个href="http://stackoverflow.com/questions/506518/is-there-any-guarantee-of-alignment-of-address-return-by-cs-new-operation/506590#506590">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.

这篇关于C ++的内存对齐问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆