c++ - 向量 push_back std::length_error: vector" 失败
问题描述
我有问题,我需要push_back
一个结构。结构是这样的。
struct RChar
{
int x;
int y;
string letter;
}
struct PResult
{
int conf;
string name;
std::vector<RChar> char_details;
};
class IResults
{
IResults(){};
~IResults(){};
float processing_time_ms;
int index;
std::vector<PResult> topPResults;
};
// use case
IResults* myres = new IResults();
PResult res;
res.conf = 30;
res.name = "xyz";
......
......
myres->topPResults.push_back(res); // here a run time exception thrown
在上面的代码中,我正在执行一个简单的操作。运行时抛出的异常是A/libc: /usr/local/google/buildbot/src/android/ndk-release-r20/external/libcxx/../../external/libcxxabi/src/abort_message.cpp:73: abort_message: assertion "terminating with uncaught exception of type std::length_error: vector" failed
. 异常看起来像向量内存分配失败。我无法准确地追踪问题是什么导致了这里的问题。
解决方案
某处内存溢出。vector
容器是一个带有指针的数据结构;如果其他代码覆盖这些指针,它会尝试访问无效内存并失败。
异常类型 ( length_error
) 可能相关,也可能不相关。想象一些错误的代码写-1
在vector
类的长度成员上——这会使vector
代码混淆,认为它的长度是无效的。
有关如何修复的详细信息,请参见此处。
推荐阅读
- r - 如何获取值第一次低于阈值(按行)的索引?
- extjs - extjs如何使用代理extraParams将数据从视图传递到存储
- python - 如何在 Numba cuda 中对字符串数组执行内核函数?
- python - 如何减少matptolib中两个条之间的空间?
- python - 在 Python3 中使用多处理进行文件读取
- java - Log4j2 自定义 Hibernate Appender 的内存泄漏
- sql - 如何按公司对结果进行分组?
- javascript - 将响应从 api 转换为具有特定格式的对象
- python - 迭代txt文件python
- c# - 为 IDBCommand 与 SqlCommand.ExecuteNonQueryAsync 实现异步 ExecuteNonQuery()