C++ 理解错误“在抛出 'std::length_error' what() 实例后终止调用:basic_string::_S_create Aborted (core dumped)”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20254996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:00:04  来源:igfitidea点击:

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

c++stringruntime-error

提问by scbeacham

So here is my error:

所以这是我的错误:

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 would just like to know what that error means so I know how to fix it. I've seen many posts with similar errors but nothing exactly the same. I am literally just starting out in C++, and none of those answers make sense with what I have learned so far. As you can see this is a simple program to output a song. It's meant to help me practice strings for the class I'm taking, but it makes absolutely no sense to me and the book isn't much help either. Could someone please explain this to me?

我只想知道这个错误意味着什么,所以我知道如何解决它。我看过很多帖子都有类似的错误,但没有完全相同的。我实际上刚刚开始使用 C++,而这些答案对我迄今为止所学的都没有意义。如您所见,这是一个输出歌曲的简单程序。它的目的是帮助我为我正在上课的课程练习弦乐,但这对我来说绝对没有意义,这本书也没有太大帮助。有人可以向我解释一下吗?

P.S. In case this is helpful, it will compile using g++, but when it runs it gives that error (so basically it's not a compile error, it's a run error).

PS 如果这有帮助,它将使用 g++ 进行编译,但是当它运行时会出现该错误(所以基本上它不是编译错误,而是运行错误)。

回答by templatetypedef

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.

您的数组长度为 9,因此其中的有效索引范围为 0、1、2、...、8。在迭代 8 中,指示的行将尝试读取无效的数组索引 9。这会导致未定义的行为,在您的情况下,这是关于无效字符串的误导性错误消息。

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!

希望这可以帮助!

回答by woolstar

If you have 9 bones, you should only print 8 connections, not 9. On the last one you reference bone[8]& bone[9]. bone[9]does not exist.

如果你有 9 个骨骼,你应该只打印 8 个连接,而不是 9 个。在最后一个你引用bone[8]& bone[9]bone[9]不存在。