In this function:
void GetFileContents(const char* FilePath, size_t& size, char* data){
file_mapping fm(FilePath, read_only);
mapped_region region(fm, read_only);
data = static_cast<char*>(region.get_address());
...
}
fm
and region
are objects with automatic storage duration, lifetime of which ends when the execution goes out of scope. Using their internals after these objects are destructed yields undefined behavior.
Another problem is that you are actually trying to change the pointer itself, meaning that your function should take char** dataPtr
and you should call GetFileContents(FilePath, size, &data);
.
But since you are using C++, you should consider using std::string
or since you're not working with null-terminated string, std::vector<char>
might be more appropriate:
void GetFileContents(const char* FilePath, std::vector<char>& content){
file_mapping fm(FilePath, read_only);
mapped_region region(fm, read_only);
content.resize(region.get_size());
char* src = static_cast<char*>(region.get_address());
memcpy(&content[0], src, content.size());
}