I retrain C ++ and I need to use memory mapped files. I decided to use boost (since it seems to be a solid library).
I created a file mapping with memory mapping into a doubling array and wrote the first double in this array. On disk, the file contained some data in the first four bytes, and the rest was zeroed out, it was interesting for me, as usual, if I get a C ++ pointer to a memory location, in most cases I should assume that it contains garbage.
Do I have guarantees that the newly created memory mapped files will be reset (at least on Linux)? I did not find any link for this.
BOOST_AUTO_TEST_CASE(OpenMMapFile){
boost::iostreams::mapped_file file;
boost::iostreams::mapped_file_params params;
params.path = "/tmp/mmaptest-1";
params.mode = std::ios::in | std::ios::out;
params.new_file_size = 10*sizeof(double);
file.open(params);
double* data = static_cast<double*>((void*)file.data());
data[0] = 12;
file.close();
}
Here is the contents of the file:
cat /tmp/mmaptest-1 | base64
AAAAAAAAKEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA =
EDIT
@Zan --- boost ftruncate mmaped, ( , Linux).