boost::shared_array 和对齐内存分配
在 Visual C++ 中,我尝试动态分配一些 16 字节对齐的内存,以便我可以使用需要内存对齐的 SSE2 函数。现在这就是我分配内存的方式:
boost::shared_array aData(new unsigned char[GetSomeSizeToAllocate()]);
我知道我可以使用 _aligned_malloc 来分配对齐内存,但是当 boost 尝试释放我的内存时,这会导致出现问题吗?这是 boost 用于释放内存的代码:
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;
}
由delete释放的内存必须用new分配,对吧?关于如何解决这个问题有什么建议吗?
In Visual C++, I'm trying to dynamically allocate some memory which is 16-byte aligned so I can use SSE2 functions that require memory alignment. Right now this is how I allocate the memory:
boost::shared_array aData(new unsigned char[GetSomeSizeToAllocate()]);
I know I can use _aligned_malloc to allocate aligned memory, but will that cause a problem with boost when it tries to free my memory? This is the code boost uses to free the memory:
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
boost::shared_array
有一个构造函数,它将删除器作为第二个参数来使用,而不是默认的delete[]
。这意味着您可以像这样传递合适的释放函数的地址。
参考文献: Boost.SharedArray
boost::shared_array
has a constructor that takes a deleter as a second argument to be used instead of defaultdelete[]
.This means you might be able to pass the address of a suitable deallocation function just like that.
References: Boost.SharedArray