Having assertion failure while deleting an element from a vector.What are the causes of assertion failure?

Lots 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 way to debug the program is to start commenting out sections of the code until the assertion failure goes away. That will let you narrow down the problem.

Hello Thankyou.Below is my code.In the x.erase(iter) i am getting that problem

        vector<x*>::iterator iter;
    for(iter=x.begin();iter!=x.end();++iter)
    {
        delete *iter;
        x.erase(iter);
    }

I debugged but couldn't understand what is the exact problem?

Are 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();

There 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 iter;
 for( iter=x.begin() ; iter!=x.end() ; iter = x.erase(iter) )
 {
     delete *iter;
 }

This is perhaps the simplest way to do it:

for( auto ptr : x ) delete ptr ;
x.clear() ;

Hi,
I dont want the entire vector to be cleared.I need only few elements to be deleted from a vector.By doing clear all my elements are getting erased?
Vijayan i tried ur code even then it is throwing assertion failure.

Make sure what you are deleting is something that should be deleted!. Anyways, the key point to take is that vector::erase returns a iterator pointing to the next element after the one deleted. If possible, put up the whole context(i.e full code )

Among other things, you may have NULL pointers in your vector, which can't be delete d. Verify what's in your vector before you worry about what's wrong with your deletion code, e.g.:

// changed to while() loop to make logic more obvious
vector<T*>::iterator iter = x.begin();
while (iter != x.end()) {
    T* ptr = *iter;
    if (! ptr) {
        // NULL pointer in your vector
    }
    else if ( /* test to see if element should be removed */ ) {
        delete ptr;
        iter = x.erase(iter);
    }
    else
        iter++;
}

Among other things, you may have NULL pointers in your vector, which can't be delete d. Verify what's in your vector before you worry about what's wrong with your deletion code, e.g.:

// changed to while() loop to make logic more obvious
vector<T*>::iterator iter = x.begin();
while (iter != x.end()) {
    T* ptr = *iter;
    if (! ptr) {
        // NULL pointer in your vector
    }
    else if ( /* test to see if element should be removed */ ) {
        delete ptr;
        iter = x.erase(iter);
    }
    else
        iter++;
}

its perfectly fine to delete a null pointer

its perfectly fine to delete a null pointer

Lucky me! This is what I get for starting with C programming, and then learning C++ later ... I'm way too careful about handling pointers! And yes, I'm old enough to pre-date a usable C++ compiler.

Thanks to everyone for suggestions.

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.