2010-10-22 182 views
0

在Visual C++中,我试图动态分配一些16字节对齐的内存,所以我可以使用需要内存对齐的SSE2函数。现在,我这是怎么分配的内存:boost :: shared_array和对齐的内存分配

boost::shared_array aData(new unsigned char[GetSomeSizeToAllocate()]);

我知道我可以使用_aligned_malloc分配对齐的存储空间,但会引起与提升一个问题,当它试图释放我的记忆?这是代码升压用来释放内存:

 
template inline void checked_array_delete(T * x) 
{ 
    typedef char type_must_be_complete[ sizeof(T)? 1: -1 ]; 
    (void) sizeof(type_must_be_complete); 
    delete [] x; 
} 

Memory free'd by delete must be allocated with new, right? Any tip on how I can get around this?

回答

1

boost::shared_array有一个构造函数有删除的第二个参数将被用来取代默认的delete[]

这意味着您可能可以像这样传递合适的释放函数的地址。

boost::shared_array<X> array(allocate_x(100), &deallocate_x); 

参考文献:Boost.SharedArray