0

我有一个struct alignedStruct,它需要特殊对齐(见分机):

void* operator new(size_t i) { return _aligned_malloc(i, 16); }
void operator delete(void* p) { _aligned_free(p); }

这种对齐方式适用于 的唯一对象/指针alignedStruct,但后来我尝试这样做:

alignedStruct * arr = new alignedStruct[count];

我的应用程序崩溃了,问题肯定是关于“数组对齐”(正好在上一行):

0xC0000005: Access violation reading location 0xFFFFFFFF.

这种崩溃发生在约 60% 的时间,也表明问题不典型。

4

1 回答 1

2

我相信您正在寻找的是放置new,它允许您_aligned_malloc正确使用构造函数。或者,您可以重载operator new[]operator delete[].

void* operator new[] (std::size_t size)
{
   void * mem = _aligned_malloc(size, 16);
   if(mem == nullptr)
      throw std::bad_alloc;
   return mem;
}
于 2013-07-31T19:29:27.020 回答