[osg-users] How to byte-align Vec4Array

Judd Tracy jtracy at ist.ucf.edu
Tue Jul 8 06:34:12 PDT 2008

You could use a STL allocator that would allow you to do the alignment, 
but the question is how do you get that to the vector.  There is no way 
to pass an allocator to the current MixinVector so you are going to have 
to modify osg to try this.  It would be nice if the new MixinVector 
class and all vectors that use it would allow for an allocator to be 
used on the underlying vector, but I guess there has never been a need 
for this yet.


David Spilling wrote:
> Hi Gordon, Thibault,
> Thanks for the replies regarding the contiguity of the memory in a 
> std::vector. That at least solves half of the problem.
>     Use &yourvector[0] to get a float* pointer to the beginning of the
>     array. 
> How do I define the vec4array so that &yourvector[0] is absolutely 
> aligned, i.e. a multiple of (in my case) 16?
> With a float array, on MS compilers, I would do __declspec(align(16)) 
> float* myarray = new float[4 * MY_ARRAY_SIZE]. From what I understand, 
> and from the MSDN documentation 
> (http://msdn.microsoft.com/en-us/library/83ythb65.aspx) this is the 
> correct way to guarantee this. #pragma pack 16 doesn't always do what 
> you expect...
> If I do __declspec(align(16)) Vec4Array* myarray = new Vec4Array, I'm 
> guaranteed that "myarray" is 16 byte aligned, but what about the 
> contents of the array? Especially if I do lots of ->push_back so that 
> the std::vector resizes and goes somewhere else. Even if I do 
> __declspec(align(16)) Vec4Array* myarary = new 
> Vec4Array(MY_ARRAY_SIZE) I don't think I'm guaranteed 16 byte 
> alignment of the first vector entry...
> (I think the gcc equivalent to __declspec(align(X)) is __aligned__(X), 
> by the way).
> Regards,
> David

More information about the osg-users mailing list