Having assertion failure while deleting an element from a vector.What are the causes of assertion failure?
sita12345 0 Newbie Poster
Recommended Answers
Jump to PostLots of different causes, one being your program may have scribbled something all over memory and damaged vector's memory space. Another problem could be bad use of pointers. Without knowing more details about the program its not possible to give you any really good answers to your question.
One …
Jump to PostAre you trying to delete a vector of pointers?
std::vector<x*>::iterator begin = x.begin(); while(!x.empty()){ x* tmp = x.back(); x.pop_back(); delete tmp; } x.clear();
Jump to PostThere is a problem with this code:
vector<x*>::iterator iter; for(iter=x.begin();iter!=x.end();++iter) { delete *iter; x.erase(iter); // once the element is erased, the iterator is no longer valid // ++iter now will cause undefined behaviour. }
Instead, you could use the value returned by erase:
vector<x*>::iterator …
All 10 Replies
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
sita12345 0 Newbie Poster
mrnutty 761 Senior Poster
vijayan121 1,152 Posting Virtuoso
sita12345 0 Newbie Poster
mrnutty 761 Senior Poster
raptr_dflo 48 Posting Pro
mrnutty 761 Senior Poster
raptr_dflo 48 Posting Pro
sita12345 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.