Reputation: 33813
I have written code to read a big size file content and write that content into a new file.
That code works fine with a small and medium size file content but with a big size file content, approximately 1.8GB and above it does not works and gives me an unknown error/exception during runtime.
Also, I have tried to debug and the following is the debugging result:
The code:
char * myClass::getFileContent(const char * fileName) {
std::ifstream file(fileName, std::ios::binary|std::ios::ate);
if (!file.is_open()) {
perror(strerror(errno));
return "";
}
char * strBuffer = NULL;
long long length = file.tellg();
strBuffer = new char[length];
file.seekg(0, std::ios::beg);
file.read(strBuffer, length);
return strBuffer;
}
// The implementation
char *fileName = "C:\\desktop-amd64.iso";
char *fileContent = myClass.getFileContent(fileName);
ofstream file("c:\\file.iso", ios::binary);
if (file.is_open()) {
file.write(fileContent, myClass.getFileSize(fileName));
file.close();
}
delete fileContent;
Note: I am using Visual Studio 2015 on Windows 7 x64.
Why does that problem happen with the big files?
Upvotes: 0
Views: 119
Reputation: 31
The debugger shows that an std::bad_alloc
exception occurs at the line
strBuffer = new char[length];
It seems that you are trying to allocate a block in memory that's size is the same as the file you are trying to read. As the file's size is around 1.8 GB, it is possible that the operating system just cannot allocate a chunk in memory with that size.
I recommend reading this answer about handling huge files without storing all their contents in the memory.
Upvotes: 3
Reputation: 188
As far i can see. You are trying to release memory with free
while you allocate memory with new
. You cannot mix them.
To deallocate memory, use delete
.
Now, if you need to free it with free
, you must allocate memory with malloc
.
Upvotes: 0