1

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 ?

Aaron McDaid
  • 26,501
  • 9
  • 66
  • 88
jokoon
  • 6,207
  • 11
  • 48
  • 85
  • 2
    What is your understanding, of the word *aligned* in this context? – Alok Save Jan 29 '12 at 12:31
  • And what do you mean by "decide of the behavior of the `new` operator"? – Fred Foo Jan 29 '12 at 12:31
  • The alignment of the allocation will not be the vectors responsibility as you do the allocation and not the vector. It is just there to hold the pointers you put into it. – Nobody moving away from SE Jan 29 '12 at 12:33
  • 1
    well I did not mean aligned, rather contiguous, I know it's not the same meaning, but I want to know if the allocated object will be placed just like an array – jokoon Jan 29 '12 at 12:38
  • 1
    So you are asking if two new statements will place the allocated objects right beneath each other? I am fairly sure that the allocater can handle this at will and that there is no guarantee for this. The only way I know to ensure that they follow each other is by allocating an array. – Nobody moving away from SE Jan 29 '12 at 12:43
  • yes but I can add an element at any time, and they won't be contiguous anymore. – jokoon Jan 29 '12 at 12:49
  • @gokoon, the members of the vector are pointers. These pointers will be stored beside each other. `vector` always guarantees that its members are contiguous, there is no doubt about that. Are you asking about the pointers, or about the *objects pointed to by the pointers*? – Aaron McDaid Jan 29 '12 at 16:01
  • I would like to recommend you the following article: [Cringe not: Vectors are guaranteed to be contiguous](http://herbsutter.com/2008/04/07/cringe-not-vectors-are-guaranteed-to-be-contiguous/). – Sergey Vyacheslavovich Brunov Jan 29 '12 at 21:49
  • yeah I read that some time ago, but's that not my concern – jokoon Jan 30 '12 at 06:41

3 Answers3

5

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?

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • `typedef btAlignedObjectArray < class btCollisionObject * > btCollisionObjectArray` this is what worries me (bullet physics) – jokoon Jan 29 '12 at 12:46
1

You want to push many T* into the vector such that the pointed-to objects are contiguous. The various T* will be beside each other, but you also want the T objects to be beside each other. If you know the number of items in advance, you can do this:

T* arr = new T[N];
for(int i = 0; i<N; ++i) {
    vect.push_back(&(arr[i]));
}

To delete, you must call delete[] vect[0];. You call delete only on the first item. If you delete the others, you'll get undefined behaviour.

(I still don't know why you want them to be contiguous.)

Aaron McDaid
  • 26,501
  • 9
  • 66
  • 88
0

The pointers will be contiguous in memory, just like a

 T* array[]

This is guaranteed by the standard. However, a vector will reallocate on sepcific operations (such as growing capacity); This too is well defined by the standard. Read up on

 std::vector<>::resize
 std::vector<>::reserve
 std::vector<>::capacity
 std::vector<>::size

As long as not reallocation is triggered, the vector can be treated just like an array.

A link: http://en.cppreference.com/w/cpp/container/vector/reserve

sehe
  • 374,641
  • 47
  • 450
  • 633