std::aligned_storage 的分配目标(堆栈还是堆?)

发布于 2024-10-09 05:53:17 字数 927 浏览 4 评论 0原文

我一直在尝试了解 TR1 添加的内容(称为aligned_storage)。在阅读以下文档N2165时, N3190N2140 我一生都看不到语句,其中清楚地描述了正在使用的内存的堆栈或堆性质。

我查看了 msvc2010、boost 和 gcc 提供的实现,它们都提供了一个基于堆栈的解决方案,以联合的使用为中心。

简而言之:

  • aligned_storage实现使用的内存类型(堆栈或堆)是否已定义,或者它始终是基于堆栈的?

  • 以及,定义/确定该内容的具体文档是什么?

注意:在MSVC10中,以下是aligned_storage类型的定义,在这种情况下,如果aligned_storage是自动变量,则在堆栈上创建data(_Val,_Pad):

template<class _Ty, size_t _Len> 
union _Align_type
{   
   // union with size _Len bytes and alignment of _Ty
   _Ty _Val;
   char _Pad[_Len];
};

注意:这是NOT 一个小问题。在发布答案之前,请尝试理解该问题。

I've been trying to get my head around the TR1 addition known as aligned_storage. Whilst reading the following documents N2165, N3190 and N2140 I can't for the life of me see a statement where it clearly describes stack or heap nature of the memory being used.

I've had a look at the implementation provided by msvc2010, boost and gcc they all provide a stack based solution centered around the use of a union.

In short:

  • Is the memory type (stack or heap) used by aligned_storage implementation defined or is it always meant to be stack based?

  • and, What the is the specific document that defines/determines that?

Note: In MSVC10, the following is the definition of the type of aligned_storage, in this case if the aligned_storage is an auto variable the data(_Val,_Pad) is created on the stack:

template<class _Ty, size_t _Len> 
union _Align_type
{   
   // union with size _Len bytes and alignment of _Ty
   _Ty _Val;
   char _Pad[_Len];
};

Note: This is NOT a trivial question. Please try and understand the question before posting an answer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

时光与爱终年不遇 2024-10-16 05:53:17

std::aligned_storage 只是声明一个成员 typedef (type)。

成员 typedef type 应是一个 POD 类型,适合用作大小至多为 Len 且对齐方式为 的除数的任何对象的未初始化存储。 >对齐

(这来自最新的 C++0x 草案 N3225,20.7.6.6 表 53,但 TR1 规范中的语言 N1836 实际上是相同的,除了在 C++0x 中 Align< /code> 模板参数的默认参数是最大对齐值。)

std::aligned_storage 本身不分配任何内存。您可以创建 std::aligned_storage::type 类型的对象,并将该对象重新解释为满足上述要求的任何类型的对象。

std::aligned_storage<Len, Align> just declares a member typedef (type).

The member typedef type shall be a POD type suitable for use as uninitialized storage for any object whose size is at most Len and whose alignment is a divisor of Align

(This is from the latest C++0x draft, N3225, 20.7.6.6 Table 53, but the language in the TR1 specification, N1836, is effectively the same except that in C++0x the Align template parameter has as its default argument the maximum alignment value.)

std::aligned_storage doesn't allocate any memory itself. You can create an object of type std::aligned_storage<Len, Align>::type and reinterpret that object as an object of any type that meets the requirements stated above.

永言不败 2024-10-16 05:53:17

您通常不需要对齐堆上的内容,因为任何分配(new/malloc)都会在与任何类型对齐的地址处返回内存。

You typically don't need to align stuff on the heap since any allocation (new/malloc) returns memory at an address which is aligned to any type.

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