我有一个问题,我需要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
。异常看起来像是向量内存分配失败。我无法准确地追踪这个问题是什么导致了这里的问题。
发布于 2021-01-05 17:00:28
某处内存溢出。vector
容器是一个带有指针的数据结构;如果其他代码覆盖了这些指针,它将尝试访问无效内存,但会失败。
异常类型(length_error
)可能相关,也可能不相关。想象一下,一些错误的代码在vector
类的长度成员上编写-1
-这会使vector
代码混淆,认为它的长度是无效的。
有关如何修复的详细信息,请参阅here。
https://stackoverflow.com/questions/65575547
复制相似问题