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

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...