【问题标题】:Memory alignment and long copies内存对齐和长拷贝
【发布时间】:2012-04-19 22:59:03
【问题描述】:

如果我们假设我将一块内存与 memalign 对齐到 4bytes ,这样做是否仍然安全:

unsigned int* source = In.Data;
unsigned int* dest = Out.Data;

int loops = In.Size / 4; //size is the same for both in/out 

while (loops)
{
    *dest++=*source++;
    loops--;
}

而不是一次复制 1 个字节?如果不是,如何判断内存是否正确对齐,以便在需要时回退到标准字节副本?

【问题讨论】:

  • 你必须将内存对齐到alignof(unsigned int),而不是“4字节”。
  • 是的,我这样做 (sizeof(unsigned int)) D:
  • 不过,一般不需要alignof(T) == sizeof(T)。例如,考虑long double 类型。

标签: c++ c memory alignment


【解决方案1】:

是的,这是安全的,因为您的块已正确对齐。

如果从malloc 获取块也是安全的,因为malloc 返回的内存块保证为任何目的正确对齐。

【讨论】:

  • 感谢您的及时回复:D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-18
  • 2012-04-12
  • 2012-06-05
  • 1970-01-01
  • 1970-01-01
  • 2015-01-13
  • 1970-01-01
相关资源
最近更新 更多