[英]C++ 'std::bad_alloc' what(): std::bad_alloc
I am trying to run the below C++ code and I get this error: Could anyone please help me clarify why is this the issue Input: input/text_4.txt 9我正在尝试运行以下 C++ 代码,但出现此错误: 谁能帮我澄清一下为什么会出现这个问题 输入:input/text_4.txt 9
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc Aborted (core dumped) After reading a few similar threads, the solution is to check dynamic memory allocation.抛出 'std::bad_alloc' 实例后调用终止 what(): std::bad_alloc Aborted (core dumped) 在阅读了一些类似的线程之后,解决方案是检查动态 memory 分配。 However, my code does not have any dynamically allocated memory
但是,我的代码没有任何动态分配的 memory
input file text_4.txt:输入文件 text_4.txt:
This is because not very many happy things happened in the lives of the three Baudelaire youngsters.这是因为波德莱尔三个年轻人的生活中并没有发生太多幸福的事情。
Input: input/text_4.txt 8输入:input/text_4.txt 8
When I run your code, on the i==16
iteration of the outer loop in arrangefile
, we get width==8
and total==10
, with check==1
.当我运行您的代码时,在
arrangefile
的外部循环的i==16
迭代中,我们得到width==8
和total==10
,并带有check==1
。 As a result, even
is initialized to -2
, and so the while(even--)
loop is (nearly) infinite.结果,
even
被初始化为-2
,因此while(even--)
循环(几乎)是无限的。 So it attempts to add spaces to low
until it runs out of memory.因此它会尝试将空格添加到
low
,直到用完 memory。
(Note that the memory used by std::string
is dynamically allocated, so your code does have dynamic memory allocation. The same for std::vector
.) (请注意,
std::string
使用的 memory 是动态分配的,因此您的代码确实具有动态 memory 分配。对于std::vector
也是如此。)
I haven't analyzed your algorithm closely enough to figure out the correct fix, but it's possible your loop should be while(even-- > 0)
instead.我没有足够仔细地分析您的算法以找出正确的修复方法,但您的循环可能应该是
while(even-- > 0)
。
I'll second the tip in the comments to use your debugger, and I'll repost the link: What is a debugger and how can it help me diagnose problems?我将在评论中提出使用您的调试器的提示,我将重新发布链接:什么是调试器以及它如何帮助我诊断问题? .
. That's how I found this bug.
这就是我发现这个错误的方式。
I ran the program under the debugger gdb
.我在调试器
gdb
下运行程序。 It ran for a few seconds, at which point I got suspicious because the program doesn't appear do anything complicated enough to take that much computation time.它运行了几秒钟,此时我开始怀疑,因为该程序似乎没有做任何复杂到需要这么多计算时间的事情。 So I interrupted the program (Ctrl-C) which let me see where it was and what it was doing.
所以我打断了程序(Ctrl-C),它让我看到它在哪里以及它在做什么。 I could see that it was within the
while(even--)
loop.我可以看到它在
while(even--)
循环中。 That was also suspicious because that loop should complete very fast.这也很可疑,因为该循环应该很快完成。 So I inspected the value of
even
(with the command p even
) and saw that it was a large negative number.所以我检查了
even
的值(使用命令p even
),发现它是一个很大的负数。 That could only happen if it had started as a negative number, which logically could only happen if total
were greater than width
.只有当它以负数开始时才会发生这种情况,这在逻辑上只有在
total
大于width
时才会发生。 Inspecting their values I could see that this was indeed the case.检查他们的价值观,我可以看到确实如此。
Maybe this will be helpful as you learn more about using your debugger.当您了解有关使用调试器的更多信息时,这可能会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.