std::aligned_storage 的分配目标(堆栈还是堆?)
我一直在尝试了解 TR1 添加的内容(称为aligned_storage)。在阅读以下文档N2165时, N3190 和 N2140 我一生都看不到语句,其中清楚地描述了正在使用的内存的堆栈或堆性质。
我查看了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
std::aligned_storage
只是声明一个成员 typedef (type
)。(这来自最新的 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
).(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 typestd::aligned_storage<Len, Align>::type
and reinterpret that object as an object of any type that meets the requirements stated above.您通常不需要对齐堆上的内容,因为任何分配(
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.