微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c – shared_ptr如何破坏对齐

我正在阅读关于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设置比认分配器更严格的对齐,那么很容易看到在使用认分配器时这会如何失败.

原文地址:https://www.jb51.cc/c/110623.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐