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
deleteon those ?Yes, You will need to call
deleteon 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?