简体   繁体   中英

vector push_back std::length_error: vector" failed

I have a problem, I need to push_back a srtuct. 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 . The exception looks like vector memory allocation failed. I am unable to trace the issue exactly what is causing the problem here.

There is a memory overrun somewhere. The vector container is a data structure with pointers; if some other code writes over these pointers, it tries to access invalid memory and fails.

The exception type ( length_error ) may or may not be relevant. 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.

See here for details on how to fix.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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