【发布时间】:2012-07-12 07:39:58
【问题描述】:
我的程序是用 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 行只是一个不受文件大小影响的小变量。
【问题讨论】:
-
有用的是“strace
”和操作系统信息。对于现代堆来说,大小并不太大,而且我很确定 Linux 上的 malloc 与股票 GCC 很容易分配空间。 -
查看
malloc的返回值。一个正确的程序可能会耗尽内存,但它永远不会出现段错误。 -
为什么不在这种情况下使用
mmap?此外,要获得快速准确的分析,请在 Valgrind 下运行程序(调试版本) -
mediafire.com/?y667x54elblca0a 这是 strace 信息。这是来自 uname Linux ubuntu 3.2.0-26-generic-pae #41-Ubuntu SMP Thu Jun 14 16:45:14 UTC 2012 i686 i686 i386 GNU/Linux 的操作系统信息
-
我认为如果你可以部分分配整个文件在内存中是不好的
标签: c memory heap-memory