Understanding the error "ending the call after calling the instance 'std :: length_error' what (): basic_string :: _ S_create Aborted (core dumped)"

So here is my mistake:

terminate called after throwing an instance of 'std::length_error' what(): basic_string::_S_create Aborted (core dumped) 

and here is my code:

 //Code removed string generateSong(string list[], int num) { //Code removed //Code removed for (i = 0; i < num; i++) { output += list[i]; output += bone1; output += list[i + 1]; output += bone2; } return output; } int main() { string list[9] = { //Code removed }; //Code removed return 0; } 

I just wanted to know what this error means, so I know how to fix it. I have seen many posts with similar errors, but nothing exactly the same. I'm literally just starting out in C ++, and none of these answers make sense with what I have learned so far. As you can see, this is a simple song output program. It helped me train the strings for the class that I take, but for me it makes absolutely no sense, and the book doesn’t help much either. Can anyone explain this to me?

PS In case it is useful, it will be compiled using g ++, but when it starts, it will give this error (so basically this is not a compilation error, this is a startup error).

+7
c ++ string runtime-error
source share
2 answers

This piece of code is suspicious:

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

Your array has a length of 9, so the valid indices in it vary from 0, 1, 2, ..., 8. At iteration 8, the specified row will try to read the index of array 9, which is unacceptable. This leads to undefined behavior, which in your case is a misleading error message on an invalid line.

You will need to decide what steps you will take to fix this, but I believe that this is the immediate cause of the problem.

Hope this helps!

+8
source share

If you have 9 bones, you should print only 8 connections, not 9. On the last you refer to bone[8] and bone[9] . bone[9] does not exist.

+2
source share

All Articles