boost::shared_array 和对齐内存分配

发布于 2024-09-28 23:59:57 字数 542 浏览 0 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

゛时过境迁 2024-10-05 23:59:57

boost::shared_array 有一个构造函数,它将删除器作为第二个参数来使用,而不是默认的 delete[]

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

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

参考文献: Boost.SharedArray

boost::shared_array has a constructor that takes a deleter as a second argument to be used instead of default delete[].

This means you might be able to pass the address of a suitable deallocation function just like that.

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

References: Boost.SharedArray

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文