I am using following code to read a file into chararcter array. Now, for small file (say for 2 MB) it is executing properly but for large file (140 MB), in my 18 GB UBUNTU server it is giving segmentation fault
. Can anybody help me how to solve this ? I think 18 GB is enough to hold a 240 MB file into memory. I am using 64 bit UBUNTU and compiling using g++.
ifstream is;
char chararray [fileSize] ;
is.read(chararray, fileSize) ;
If the array is a local variable you will get a stack overflow, as it will not fit on the stack. Allocate the "array" on the heap instead, either directly using new
or indirectly by using std::vector
.
Or use memory mapping. See the mmap
function.
Instead of allocating the char array on the stack, I'd try using std::vector
, which will allocate dynamically on the heap:
std::vector<char> buffer(fileSize);
is.read(&buffer[0], fileSize);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With