Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

using remove method of std::list in a loop is creating segmentation fault

I am using remove() of std::list to remove elements in a for loop. But it is creating segmentation fault. I am not using iterators. Program is given below.

#include <iostream>
#include <list>

using namespace std;

int main() {
    list <int> li = {1, 2, 3, 4, 5};
    
    for(auto x : li)
    {
        if (x == 4) {
            li.remove(x);
        }
    }

    return 0;
}

In case of iterators, I understand that iterators are invalidated if we remove an element and we need to take care of incrementing iterator properly. But here I am not using iterators and I am using remove() which doesn’t return any. Can any one please let me know if we can’t use remove in a loop or if there is any issue with the code.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

But here I am not using iterators and I am using remove() which
doesn’t return any. Can any one please let me know if we can’t use
remove in a loop or if there is any issue with the code.

You are mistaken. Range based for loops are based on iterators. So after this call

li.remove(x);

the current used iterator becomes invalid.

For example in C++ 17 the range-based for loop is defined the following way (9.5.4 The range-based for statement)

1 The range-based for statement

for ( for-range-declaration : for-range-initializer ) statement

is equivalent to

{
    auto &&__range = for-range-initializer ;
    auto __begin = begin-expr ;
    auto __end = end-expr ;
    for ( ; __begin != __end; ++__begin ) {
        for-range-declaration = *__begin;
        statement
    }
}

Pay attention to that using the rangge-based for loop to call the member function remove does not make a great sense because the function will remove all elements with the given value.

If the compiler supports C++ 20 then you can just write

std::erase( li, 4 );
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading