vector push_back problem.

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?
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.
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.
As someone said, just using clear to clear every elements in Vector is not sufficient.


Someone was wrong.
Function clear is equivalent to erase(vi.begin(), vi.end()). So there is no need to call in fact the same function two times.
I got wrong information.
Thanks to correct my wrong data in my head.
Topic archived. No new replies allowed.