0

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

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. However, my code does not have any dynamically allocated memory

input file 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

  • 3
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5910058) – Jesper Juhl Apr 18 '20 at 14:55
  • 4
    You use `std::vector`. It's a "dynamic array" which allocates and reallocates dynamically as needed. – Some programmer dude Apr 18 '20 at 14:55
  • How am I supposed to solve this issue ? – Vaishnavi Ganseh Apr 18 '20 at 14:59
  • 2
    The first comment told you how. That is the first thing a programmer should do in the situation. Use a debugger to figure out the source of the problem. – drescherjm Apr 18 '20 at 15:01
  • 2
    You have `unsigned found = filepath.find_last_of("/")` and then `filepath.substr(found + 1)` *without* checking the value of `found`. What if `filepath` doesn't contain a '/' character? – G.M. Apr 18 '20 at 15:02
  • @G.M.. It always has ''/" – Vaishnavi Ganseh Apr 18 '20 at 15:04
  • 1
    One possible cause is if one of your accesses to the `scale` vector is out of bounds. Using `at()` instead of `operator[]` would help you catch that. – Nate Eldredge Apr 18 '20 at 15:04
  • @Someprogrammerdude am I supposed to delete the vector ? – Vaishnavi Ganseh Apr 18 '20 at 15:04
  • ***am I supposed to delete the vector ?*** No. – drescherjm Apr 18 '20 at 15:04
  • 1
    Note that I am just guessing; without your input file I can't run your code to attempt to reproduce the problem. So if you want more specific help, post an input file that triggers the crash. On a similar note, it'd be courteous to post the *complete* source code, including the `#include` lines, so that a person can compile and run your code verbatim without having to tweak it. – Nate Eldredge Apr 18 '20 at 15:06
  • @NateEldredge I have updated my post and included the header files and the input :) Sorry about not including it earlier – Vaishnavi Ganseh Apr 18 '20 at 15:11

1 Answers1

4

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. As a result, even is initialized to -2, and so the while(even--) loop is (nearly) infinite. So it attempts to add spaces to low until it runs out of 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.)

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.


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. 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. I could see that it was within the while(even--) loop. 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. That could only happen if it had started as a negative number, which logically could only happen if total were greater than 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.

Nate Eldredge
  • 36,841
  • 4
  • 40
  • 60