-1

This questions has good answers on how to write an std::vector into a file: Reading and writing a std::vector into a file correctly

In my case, I have a vector of arrays:

vector<array<double, 3> > vec;

I would like to write into a file in order to get a file having the following format, where the values are doubles and the first number is the position in the vector and the second is the position in the array:

vec0_0 vec0_1 vec0_2 vec1_0 vec1_1 vec1_2 vec2_0 ...

Can I just use...

std::copy(vec.begin(), vec.end(), std::ostreambuf_iterator<char>(FILE));

...or...

size_t sz = vec.size();
FILE.write(reinterpret_cast<const char*>(&vec[0]), sz * sizeof(vec[0]));

...as proposed in the mentioned question for a scalar type, or do I need to do it differently because the type in the vector is an array?

Michael
  • 7,407
  • 8
  • 41
  • 84

1 Answers1

1

From what I understand, std::array has contiguous storage. However, I don't think that guarantees there is no padding. If that were just a double[3], it would work out of the box, but I think you'd have to test very carefully and worry about portability with a std::array inside the container.

In fact, looking around there is already an example out there of a system that pads.

std::array alignment

sizeof(int) = 4;  
sizeof( std::tr1::array< int,3 > ) = 16;  
sizeof( std::tr1::array< int,4 > ) = 16;    
sizeof( std::tr1::array< int,5 > ) = 32;

Presumably this padding is implementation defined, or maybe you can find it in the standard somewhere. In any case, I'd just iterate the thing or use a non-stl array.

I'd guess the concept is similar to a struct where there is often padding introduced to optimize memory access, however the compiler is optimizing that padding, and it can be turned off on most compilers with #pragma pack statements. Not true of stl containers to my knowledge.

Josh
  • 12,602
  • 2
  • 41
  • 47
  • Does padding actually matter, if the reverse code is used to get the values back from that file? (let's assume the same target platform and compiler) – user0042 Aug 25 '17 at 17:34
  • @user0042: No, padding or whatever additional data would not matter in that case. But I'm asking for a specific resulting file layout in the question. – Michael Aug 25 '17 at 17:35
  • Yeah I mean, bottom line, you probably don't need to write the thing all at once unless your profiler is telling you this is the bottleneck in your application. If it is somehow was the hotspot and the boss was breathing down your neck I'd refactor and use a non-stl type internally. No harm no foul. Otherwise favor readability and maintainability so the next guy that comes down the path doesn't have to figure out what magic padding nonsense is going on. – Josh Aug 25 '17 at 17:38