运行时报错:
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::substr
Aborted (core dumped)
内存读取越界。
解释1:for example:
const std::string sTest( "test" );
sTest.substr( 0, 10 );
will raise the same exception, since you ask 10 characters, but only 5 ( sTest.length()) ) are available.
调试过,正常运行,无报错。
解释2:
Chances are you did something like:
std::string s("foo");
s.substr(5,1); //the length of the string is 3, 5 is out of bounds
调试过,确实会报错,out_of_range
本文探讨了在C++中遇到内存读取越界错误的原因,并提供了实例解析和调试方法。通过分析错误信息,如'terminate called after throwing an instance of std::out_of_range'和'Aborted (core dumped)',我们了解到这通常是由尝试访问超出字符串长度的内存区域导致的。文章详细解释了两种可能导致此类错误的情况,并提供了有效的调试步骤。
1057





