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
newor indirectly by usingstd::vector.Or use memory mapping. See the
mmapfunction.