Understanding error “terminate called after throwing an instance of ‘std::length_error’ what(): basic_string::_S_create Aborted (core dumped)”

This part of the code is suspicious:

 for (i = 0; i < num; i++)
 {
    output += list[i];
    output += bone1;
    output += list[i + 1]; // <--- here
    output += bone2;
 }

Your array has length 9, so the valid indices in it range from 0, 1, 2, …, 8. On iteration 8, the indicated line will try to read array index 9, which isn’t valid. This results in undefined behavior, which in your case is a misleading error message about an invalid string.

You’ll have to decide what steps you’ll want to take to fix this, but I believe this is the immediate cause of the problem.

Hope this helps!

Leave a Comment