我的程序是用 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 行只是一个不受文件大小影响的小变量。