Feb 1, 2013 at 3:12am UTC
I am adding int type into vector called "vi" several times like this.
std::vector<int> vi;
void addFunc(int *a, int cnt)
{
vi.erase(vi.begin(), vi.end());
vi.clear();
for(int i=0; i<cnt; i++)
vi.push_back(a[i]);
}
and I call the addfunc N times every sec.
And sometimes it has runtime error on vi.push_back line.
(called stack says "_CrtIsValidHeapPointer")
i don't know how come it has that problem.
any idea?
Feb 1, 2013 at 3:19am UTC
I think it is more likely to be a problem with either a or cnt being inaccurate or invalid than a problem with the vector.
You can get rid of the erase call, by the way. clear is sufficient.
Feb 1, 2013 at 9:13am UTC
The first time, I did just call clear func.
As someone said, just using clear to clear every elements in Vector is not sufficient. should call erase before clear. So i coded like that.
And I am looking it out to find error. Thanks anyways.
Feb 1, 2013 at 11:18am UTC
Function clear is equivalent to erase(vi.begin(), vi.end()). So there is no need to call in fact the same function two times.
Feb 4, 2013 at 2:07am UTC
I got wrong information.
Thanks to correct my wrong data in my head.