简体   繁体   English

向量 push_back std::length_error: vector" 失败

[英]vector push_back std::length_error: vector" failed

I have a problem, I need to push_back a srtuct.我有一个问题,我需要push_back一个结构。 structure is like that.结构是这样的。

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  

In above code i am performing a simple operation.在上面的代码中,我正在执行一个简单的操作。 the exception thrown on run time is 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 .运行时抛出的异常是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 The exception looks like vector memory allocation failed.异常看起来像向量 memory 分配失败。 I am unable to trace the issue exactly what is causing the problem here.我无法准确地追踪问题是什么导致了这里的问题。

There is a memory overrun somewhere.某处有 memory 溢出。 The vector container is a data structure with pointers; vector容器是一个带有指针的数据结构; if some other code writes over these pointers, it tries to access invalid memory and fails.如果其他代码覆盖这些指针,它会尝试访问无效的 memory 并失败。

The exception type ( length_error ) may or may not be relevant.异常类型 ( length_error ) 可能相关也可能不相关。 Imagine some faulty code wrote -1 over the length member of the vector class - this would confuse the vector code to think its length is invalid.想象一些错误的代码在vector class 的长度成员上写了-1 - 这会使vector代码混淆,认为它的长度是无效的。

See here for details on how to fix.有关如何修复的详细信息,请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM