0

When I use this, I guess it's safe to say those objects are aligned:

std::vector<object_type> vect;

I spotted an allocator in bullet physics, and I don't know how they work. This also raises the question about std::vector.

In the demo here, line 42, http://code.google.com/p/bullet/source/browse/trunk/Demos/BasicDemo/BasicDemo.h#42

btAlignedObjectArray<btCollisionShape*> m_collisionShapes;

the type is a pointer, and later those pointer are assigned a new. Does it really guarantee alignment ? If the allocator is made to deal with pointers, I guess yes, but I don't have any allocator knowledge, on top of that I don't know what is obsolete or not.

What about std::vector ? If I declare

std::vector<object_type*> vect;

and assign later, will the compiler still align my objects ?

jokoon
  • 6,207
  • 11
  • 48
  • 85

2 Answers2

1

The pointers will be aligned contiguously in this vector of pointers. For the objects these pointers point to, nothing can be said. They may be placed anywhere.

A striking example would be

object_type on_stack;
vect[0] = new object_type;
vect[1] = & on_stack;
vect[2] = new object_type;

where the first and third element of the vector are assigned pointers to objects instantiated with new on the heap, while the second element is assigned the address of yet another instance on the stack.

If you wish to "align" N objects on the heap, there is still new object_type[N];

s.bandara
  • 5,636
  • 1
  • 21
  • 36
-2

Do you mean, will the elements be packed contiguously?

Yes, they are guaranteed to be.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055