这会不会code让那些矢量一致?(Will this code make those vector aligned?)

235 IT屋

Let's say I have:

vector<T *> vect;

I know it's not the obvious way to go, but I have an API which does that, so I can't decide to use vector<T> instead.

so when I put object in it I do:

vect.push_back(new T);

Will this code make sure that this vector is aligned contiguous since it's a vector of pointer? will this container decide of the behavior of the new operator ?

EDIT: will I also need to call a delete on those ?

解决方案

Will this code make sure that this vector is aligned since it's a vector of pointer?

Not sure what is your understanding of aligned in this context but the situation is similar to a array of pointers where each pointer is allocated dynamic memory. The array of pointers is located in contiguous locations and each of them points to a memory located somewhere on the heap.
The same applies here in case of vector of pointer elements.

Will I also need to call a delete on those ?

Yes, You will need to call delete on each of those elements explicitly.
Standard Library containers do not take the responsibility of deallocating memory of elements in case the elements are pointers.

Good Read:
Does vector::erase() destroy the removed object?

让我们说我有:

 矢量&lt; T *&GT; VECT;
 

我知道这不是明显的路要走,但我有一个API,它这样做,所以我不能决定使用矢量&lt; T&GT; ,而不是< / P>

所以,当我把对象是我做的:

  vect.push_back(新T);
 

这会不会code确保这个载体是<打击>对齐连续的,因为它是指针的载体?将这个容器中的新经营者的行为决定的吗?

编辑: 将我还需要调用一个删除那些?

解决方案

请问这个code确保该矢量一致,因为它是指针的载体?

不知道什么是对的对齐的你在这方面的认识,但情况类似,每个指针分配动态内存指针的数组。指针数组位于连续位置和他们每个人的指向位于所述堆某处的存储器。
如果指针元素的矢量相同的适用于这里。

请问我还需要调用一个删除那些?

是的,你需要调用删除的各个元素明确。
标准库容器不参加的情况下元素是指针重新分配元素的记忆的责任。

好读:
是否向量::擦除()销毁删除对象?

本文地址:IT屋 » 这会不会code让那些矢量一致?