我的程序在C语言中,并且正在使用gcc进行编译。我正在读取文件,并将文件的内容存储到缓冲区中。为此,我需要缓冲区与文件一样大。我正在使用malloc()为缓冲区分配内存。不幸的是,我遇到了一个277MB的文件。堆太多了吗?我在运行时遇到段错误,但没有更多的信息。它适用于最大160 MB的文件,但是277MB文件的单个异常值正在破坏它。
编辑:valgrind给我
@ 0xC0000022L valgrind给了我
==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)
但在第85行只是一个小变量,不受文件大小的影响。
最佳答案
不幸的是,我不能给您一个可靠的“为什么”,但是mmap2(似乎是malloc在您的系统上调用的)只是报告它的内存不足。在这种情况下,Malloc返回NULL将导致段错误。
munmap(0xb7706000, 4096) = 0
mmap2(NULL, 2557403136, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = -1 ENOMEM (Cannot allocate memory)
作为反例,我有一个成功的玩具程序:
mmap(NULL, 283652096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f2d00994000
我会检查系统上可用的内存或程序正在使用的内存。也许是内存泄漏严重?
关于c - 堆太大了,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11453802/