I need to create a chunk of memory tightly filled with floats. But instead of making a vector of floats, I'd like to make a vector in which each member is a group of related floats:
struct FloatStruct
{
FloatStruct() :
a(1.f), b(10.f), c(100.f), d(1000.f), e(10000.f) { }
float a;
float b;
float c;
float d;
float e;
};
int main()
{
std::vector<FloatStruct> fvec(100);
auto pf = &fvec[0].a;
for(int i = 0; i < 500; ++i)
std::cout << *pf++ << "\n";
}
The above code apparently works fine on my architecture with MSVC (and also on some online compilers with GCC or Clang). The floats are all tightly packed. But I'm worried about the portability of that code. Maybe a padding could be added on some architecture and thus it would break the tightness of the memory.
Does the standard establish any guarantee about such a case?