I'm re-learning C++, and I need to use memory mapped files. I decided to use boost (since it seems to be solid library).
I created a memory mapped file mapping to an array of doubles, and wrote to first double in this array. On disk file contained some data in first four bytes, and rest of this were zeroed, this was curious for me as generally if I obtain a pointer in C++ to memory location, in most cases I have to assume that it contains garbage.
Do I have any guarantees that newly created memory mapped files will be zeroed (at least on Linux)? I didn't find any reference for that.
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 file contents:
cat /tmp/mmaptest-1 | base64
AAAAAAAAKEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
EDIT
As @Zan pointed --- boost actually uses ftruncate to resize mmaped files, so zeroing is guaranteed (at least on Linux).