stl - terminate called after throwing an instance of 'std::length_error'

前言

别人的一个demo崩掉了,崩溃的报错如下

			terminate called after throwing an instance of 'std::length_error'
			what():  basic_string::append
			已放弃

从报错提示看,错误发生在使用std::string::append()
没去看demo, 只是想找一下, 如何错误的使用std::string::append(),会发生以上报错。
查资料,做实验

  • 在网上找了一种std::vector操作时报std::length_error
  • 自己试出一种string& append (const char* s, size_t n);的情况。
    别人的demo, 不去看了,时间来不及,只能给错实验模拟错误的过程和结果. 再给一些调试建议。

实验

// @file main.cpp
// @brief 验证以下bug的产生方式
/*
terminate called after throwing an instance of 'std::length_error' what():  basic_string::append Aborted
*/
// @note 
// 	实验环境:
//	debian8.8

#include <stdlib.h> // for EXIT_SUCCESS
#include <stdio.h> // for snprintf
#include <string>
#include <string.h>
#include <stdint.h>
#include <vector>
#include <stdexcept>

#include "sys_log.h"

int main(int argc, char** argv)
{
	int i_rc = EXIT_FAILURE;
	std::string str = "";
	std::string str_to_append = "hello world";

	std::vector<int> myvector;
	
	uint64_t uint64_str_max_size = str.max_size();

	argc = argc; // guard -Werror=unused-parameter
	argv = argv;

	do {
		MYLOG_D(">> main");
		MYLOG_D("uint64_str_max_size = 0x%lx", uint64_str_max_size); // uint64_str_max_size = 0x3ffffffffffffff9

//		try {
			myvector.resize(0);

			// terminate called after throwing an instance of 'std::length_error'  what():  vector::_M_fill_insert
			// catch std::length_error, e.what() = vector::_M_fill_insert
		    // myvector.resize(myvector.max_size()+1);

			/*
				// http://www.cplusplus.com/reference/string/string/append/
				string (1)	
					string& append (const string& str);
				substring (2)	
					string& append (const string& str, size_t subpos, size_t sublen);
				c-string (3)	
					string& append (const char* s);
				buffer (4)	
					string& append (const char* s, size_t n);
				fill (5)	
					string& append (size_t n, char c);
				range (6)	
					template <class InputIterator>
						string& append (InputIterator first, InputIterator last);
			*/

			// fill (5)	
			str.append(-1, (char)0x0); // catch std::length_error, e.what() = basic_string::append Aborted

			/*
			terminate called after throwing an instance of 'std::length_error'
			what():  basic_string::append
			已放弃
			*/
//		}

/*
		// 这样catch, 可以抓住stl抛出的std::length_error类型的异常
		catch (const std::length_error& e) {
			MYLOG_D("catch std::length_error, e.what() = %s", e.what());
		}
		*/

		i_rc = EXIT_SUCCESS;
	} while (0);

	MYLOG_D("bye");
	
	return i_rc;
}

2019-0218-1318

同事实验出一种抛出std::length_error的错误使用方法.

// @file test_string.cpp
// @brief 测试std::string出现std::length_error的情况

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <string>

int main(int argc, char** argv)
{
	argc = argc;
	argv = argv;

	std::string str = "";
	str.append("hello vs2017", -1);// throw std::length_error, vs2017 和 gcc 都报 std::length_error

	/*
	0x760A18A2 处(位于 test_string.exe 中)有未经处理的异常: Microsoft C++ 异常: std::length_error,位于内存位置 0x010FF724 处。 出现了
	*/

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值