大输入时的分段错误
我知道分段错误
意味着该进程试图访问某些不允许的内存。
我正在运行其他人使用 C++ 编写的一些程序。当我的输入很大(大约1GB)时,即使我请求30GB内存,也会出现分段错误;而当输入尺寸很小时,效果很好。
那我该怎么办呢?是因为内存不够吗?我确实是一个新手,对 C++ 没有太多了解。我什至不知道代码的哪一部分控制内存分配。
感谢 BLender,调试得到的行是:
程序收到信号 SIGSEGV,分段错误。 _IO_vfscanf_internal()中的0x0000003fbd653174 来自/share/bin/intel/cc/10.1.015/lib/tls/x86_64/libc.so.6
I know segmentation fault
means that the process has attempted to access certain memory that it's not allowed to.
I'm running some program written by others using C++. And when my input is large (around 1GB), there'll be a segmentation fault even though I requested 30GB memory; while when input size is quite small, it goes well.
So what shall I do? Is it because there's not enough memory? I'm really kind of a newbie without much knowledge of C++. I don't even know which part of the code controls memory allocation.
Thanks to BLender, line from the debugging is:
Program received signal SIGSEGV, Segmentation fault.
0x0000003fbd653174 in _IO_vfscanf_internal ()
from /share/bin/intel/cc/10.1.015/lib/tls/x86_64/libc.so.6
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的代码多次调用
malloc
,但从未调用free
,因此它使用了相当多的内存。而且它从不检查内存不足的情况...我的建议是您将对 malloc 的所有调用更改为以下内容:
然后看看会发生什么。
Your code calls
malloc
several times, but neverfree
, so it uses quite a lot of memory. And it never checks for an out-of-memory condition...My suggestion is that you change all the calls to malloc to something like:
And see what happens.
您有 30GB 内存吗?我真的对此表示怀疑。
答案取决于程序的功能。如果程序在没有内存的情况下读取和处理数据(即之前读取的数据不影响正在读取的数据的处理),则可以分块加载文件。
但由于没有细节,我不能说更多。
调试你的程序。编译它时,启用调试:
并使用 gdb 对其进行调试:
并且应该显示导致段错误的行号和函数。
Do you have 30GB of memory? I really doubt it.
The answer depends on the function of the program. If the program reads and processes data without memory (i.e., the data that was read before doesn't influence the processing of the data being read), you can load the file in chunks.
But without details, I can't say much more.
Debug your program. When you compile it, enable debugging:
And use
gdb
to debug it:And the line number and function that caused the segfault should show up.
它们很可能在那里有一个固定大小的缓冲区,而您的“大输入”太大而无法容纳在其中。 C++ 默认情况下不检查这些东西(如果他们使用了来自 STL 的良好检查数据结构,但显然他们没有这样做)。
Most likely they have a fixed-sized buffer in there, and your "large input"s are too large to fit inside it. C++ does not check these things by default (if they had used nice checked data structures from the STL perhaps, but clearly they didn't do that).
尝试在调试器中运行该程序,但首先确保它是使用调试信息(-g)进行编译的。使用它所存储的数据来运行它。您的程序可能尝试通过调用 malloc 或 new 来分配大量内存并将其分配给指针,不检查是否成功,然后尝试访问假定的内存(通过指针)待分配。您可以通过在调试器中发生分段冲突后检查堆栈跟踪来了解发生这种情况的位置。这应该给您一个线索,告诉您应该修改程序的哪一部分,例如它不会在循环中读取整个输入文件,而只会读取其中的一部分。
Try running the program in a debugger, but first make sure it is compiled with debugging information (-g). Run it with the data it segvaults on. It may be that your program tries to allocate large amount of memory with a call to malloc or new and assign it to a pointer, doesn't check if that was successful and then tries to access the memory (via the pointer) which was supposed to be allocated. You would see where it happens by examining stack trace after the segmentation violation in the debugger. That should give you a clue which part of the program should be modified so e.g. it doesn't read in the whole input file but only a chunk of it, in a loop.