STL容器分配放置新 [英] STL Containers allocation placement new

查看:163
本文介绍了STL容器分配放置新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到这个问题的确切答案,因此在这里发布. 当我想到矢量时,它需要在连续的内存位置中构建对象.这意味着矢量将保持分配的内存,并且必须对被推入其中的对象进行就地构造(=放置新的).这是一个有效的假设吗?另外,这是否意味着容器正在手动调用析构函数,而不是调用delete?我在这里还缺少其他假设吗?这是否意味着我可以假设,即使我选择编写该对象,即使为该对象编写的自定义的new也可能不会被调用?

I couldn't find an exact answer to this question and hence posting here. When I think of vector, it needs to build objects in a contiguous memory location. This means that vector keeps memory allocated and have to do an in-place construction (=placement new) of objects being pushed into it. Is this a valid assumption? Also, does this mean the container is manually invoking the destructor rather than calling delete? Are there any other assumptions that I am missing here? Does this mean I can assume that even a custom written new for the object may not be invoked if I chose to write?

另外,由于我们不需要连续的内存保证,因此列表使用新的和删除的也是有意义的.那么,这种行为是驱动分配器行为的原因吗?请帮忙. 谢谢

Also it makes sense for a list to use a new and delete as we don't need the continuous memory guarantee. So, is this kind of behavior is what drives how allocators behave? Please help. Thanks

推荐答案

这意味着vector会保持分配的内存,并且必须对被推入其中的对象进行就地构造(= placement new).这是一个有效的假设吗?

This means that vector keeps memory allocated and have to do an in-place construction (=placement new) of objects being pushed into it. Is this a valid assumption?

还,这是否意味着容器正在手动调用析构函数,而不是调用delete?

Also, does this mean the container is manually invoking the destructor rather than calling delete?

我在这里还缺少其他假设吗?这是否意味着我可以假设,即使我选择编写该对象,即使为该对象编写的自定义的new也可能不会被调用?

Are there any other assumptions that I am missing here? Does this mean I can assume that even a custom written new for the object may not be invoked if I chose to write?

是的.考虑到即使在链表中,容器也不会分配您的类型的实例,而是分配包含该类型的子对象的模板化结构.对于将是某种复杂类型的链表,它至少包含两个指针(两个链接)和该类型的子对象.分配的实际类型是 node ,而不是您的类型.

Yes. Consider that even in linked lists, the container will not allocate an instance of your type, but rather a templated structure that contains a subobject of the type. For a linked list that will be some complex type containing at least two pointers (both links) and a subobject of your type. The actual type that is allocated is that node, not your type.

另外,由于我们不需要连续的内存保证,因此列表也可以使用新的和删除的内容.

Also it makes sense for a list to use a new and delete as we don't need the continuous memory guarantee.

可以,但不会new/delete个您所类型的对象.

It does, but it does not new/delete objects of your type.

那么,这种行为是驱动分配器行为的原因吗?

So, is this kind of behavior is what drives how allocators behave?

我不太了解问题的这一部分.分配器是在标准中定义的一组约束的类,包括接口(allocatedeallocate ...)和语义(==的含义是分配给一个分配的内存可以释放)另一个,则该类中的任何其他状态都是不相关的).

I don't really understand this part of the question. Allocators are classes that have a set of constraints defined in the standard, that include both the interface (allocate, deallocate...) and semantics (the meaning of == is that memory allocated with one can be deallocated with the other, any other state in the class is irrelevant).

分配器并将其传递到容器,包括效率(如果您仅分配一种类型的对象,那么您可能能够实现比malloc略高效率的小型块分配器-否则,视情况而定.)

Allocators can be created and passed onto containers for different reasons, including efficiency (if you are only allocating a type of object, then you might be able to implement small block allocators slightly more efficient than malloc --or not, depends on the situation).

关于新展示位置的注释

我总是发现有趣的是, placement new 这个词似乎有两个不同的含义.一方面是就地构建对象 的唯一方法.但这似乎也具有完全不同的含义:构造此对象从自定义分配器获取内存.

I have always found interesting that placement new is a term that seems to have two separate meanings. On the one side is the only way of constructing an object in-place. But it seems to also have a complete different meaning: construct this object acquiring memory from a custom allocator.

实际上, placement new 的唯一含义与就地构建 无关.第一种只是第二种情况,其中分配器由18.4.1.3中定义的实现(编译器)提供,并且不能重载.特定版本的重载分配器除了返回参数(void*)之外什么都不做,因此 new-expression 可以将其传递到构造函数中,并在由(称为 placement new 的版本.

In fact there is a single meaning of placement new that has nothing to do with constructing in-place. The first is just a case of the second, where the allocator is provided by the implementation (compiler) as defined in 18.4.1.3 and cannot be overloaded. That particular version of the overloaded allocator does absolutely nothing but return the argument (void*) so that the new-expression can pass it into the constructor and construct the object on the memory (not) allocated by the placement new version that was called.

这篇关于STL容器分配放置新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆