My program is in C and I’m compiling with gcc. I’m reading in a file, and storing the contents of the file into a buffer. To do that I need the buffer to be as large as the file. I’m using malloc() to allocate memory for the buffer. Unfortunately, I ran into a file that’s 277MB. Is that too much for the heap? I’m getting a seg fault at run time, but no more information than that. It’s worked for files as large as 160 MB, but this single outlier of a 277MB file is breaking it.
EDIT: valgrind gives me
@0xC0000022L valgrind gives me
==6380== Warning: set address range perms: large range [0x8851028, 0x190e6102) (undefined)
==6380== Warning: set address range perms: large range [0x8851028, 0x190e6028) (defined)
==6380== Warning: set address range perms: large range [0x190e7028, 0x2997c108) (undefined)
==6380== Warning: set address range perms: large range [0x190e7028, 0x2997c028) (defined)
==6380== Warning: silly arg (-1737565464) to malloc()
==6380== Invalid write of size 4
==6380== at 0x8048A49: main (newanalyze.c:85)
==6380== Address 0x4a00 is not stack'd, malloc'd or (recently) free'd
==6380==
==6380==
==6380== Process terminating with default action of signal 11 (SIGSEGV)
==6380== Access not within mapped region at address 0x4A00
==6380== at 0x8048A49: main (newanalyze.c:85)
but at line 85 is just a small variable that shouldn’t be affected by the size of the file.
Unfortunately I can’t give you a solid “why,” but mmap2, which appearas to be what malloc is calling on your system, simply reports it’s out of memory. Malloc will, in this case, return NULL cause a segfault.
As a counter example, I have a toy program that succeeds with:
I would check the memory available on the system or that the program is using. Maybe it’s leaking memory badly?