" isn't aligned to a 16-byte boundary so the compiler will choose a boundary for you, which may not be 16-bytes. You must explicitly tell the compiler that "Vec3
", too, must be 16-byte aligned. Also, those dummy
data-members are not doing anything useful, so you may as well remove them.
As for your segmentation-fault, "std::malloc( )
" is guaranteed to return an aligned pointer, but the boundary to which the allocated memory is aligned is system-specific; therefore, it's not safe to assume memory allocated with "std::malloc( )
" is 16-byte aligned. What you could do, with a little work, it allocate the memory and then find the first 16-byte aligned address within the allocated block yourself, like so:
void *Memory_( std::malloc( ( 3 * sizeof( float ) ) + 16 ); // 16 is the alignment
float *AlignedMemory_( static_cast< float * >( ( char * )Memory_ + ( ( uintptr_t )Memory_ % 16u ) ) );
std::free( Memory_ );
I haven't tested this code, so your mileage may vary wildly. Note that "uintptr_t
" is a C++11 addition so your current compiler may not define it. The code is pretty simple: enough memory is allocated to store at least 3 "float
"s plus an additional 16 bytes. The additional bytes are used to ensure that the first "float
" can be safely aligned to a 16-byte boundary. Then, the initial address pointed-to by "Memory_
" is rounded up to the next 16-byte boundary where the first "float
" will be stored. We keep "Memory_
" around so that we know where the allocated memory began.