加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – shared_ptr如何破坏对齐

发布时间:2020-12-16 03:42:05 所属栏目:百科 来源:网络整理
导读:我正在阅读关于DirectXMath的文档,并偶然发现了下一篇文章: As an alternative to enforcing alignment in your C++ class directly by overloading new/delete,you can use the pImpl idiom. If you ensure your Impl class is aligned via __aligned_mall
我正在阅读关于DirectXMath的文档,并偶然发现了下一篇文章:

As an alternative to enforcing alignment in your C++ class directly by
overloading new/delete,you can use the pImpl idiom. If you ensure
your Impl class is aligned via __aligned_malloc internally,you can
then freely use aligned types within the internal implementation. This
is a good option when the ‘public’ class is a Windows Runtime ref
class or intended for use with std::shared_ptr<>,which can otherwise
disrupt careful alignment.

我不明白shared_ptr如何在对齐策略中做任何改变,它只有一个指针,它不会分配一个对象.

解决方法

你是对的,std :: shared_ptr不会影响对齐.它只接受指向已经分配的对象的指针,因此如果该分配导致对象未对齐,则问题不在于std :: shared_ptr,而是与该分配有关.

但是std :: shared_ptr经常与std :: make_shared一起使用.的std :: make_shared< T>执行单个分配以为std :: shared_ptr控制结构和T实例保留内存.这种分配不是使用任何特定于类的运算符new(并且不应该).如果特定于类的运算符new设置比默认分配器更严格的对齐,那么很容易看到在使用默认分配器时这会如何失败.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读