Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4115698
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:36:56+00:00 2026-05-20T22:36:56+00:00

all. I’m working on software to block ads using the system’s hosts file, but

  • 0

all. I’m working on software to block ads using the system’s hosts file, but on to the code.

I have a custom class HostEntry that contains necessary information such as the destination host, the host to block, the destination host’s ip… etc.

In the HostsManager class, it keeps a vector to keep track of all the hosts added. In order to block a host completely, I must add example.com AND www.example.com, but when I iterate over the vector it will only delete the entry starting with “www.” and leaves the one without. If you try to delete it a second time (with only the entry missing the “www.”) it segfaults, and I don’t know why.

void HostsManager::delHost(std::string blockedhost) {
    strip(blockedhost);
    string tmp; // yes I know it's not great practice to do it like this, but it was for debug reasons
    for (vector<HostEntry>::iterator viter = hosts.begin(); viter != hosts.end(); ++viter) {
        tmp = viter->getHost();
        if (tmp == blockedhost || tmp == ("www." + blockedhost)) {
            viter = hosts.erase(viter);
        }
    }
}

An example call to that specific function:

HostsManager mgr;
mgr.delHost("mysite.com"); // this deletes "www.mysite.com" but not "mysite.com" - whether or not you call delHost() with the "www." prefix
mgr.delHost("mysite.com"); // if you call it a second time, it segfaults O.o

Help with this would be GREATLY appreciated.

EDIT: I assigned the value returned from the call to erase() to viter, same result. I still have no idea why this is happening.

If you need all the code, it’s at http://paste.pocoo.org/show/363051/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T22:36:57+00:00Added an answer on May 20, 2026 at 10:36 pm

    It is generally better to use std::remove_if to delete multiple items from a vector; it runs in linear time instead of quadratic and avoids having to worry about iterator invalidation.

    Might look something like this:

    hosts.erase(std::remove_if(hosts.begin(), 
                               hosts.end(), 
                               [&blockedhost](const HostEntry& entry) {
                                   return entry.getHost() == blockedhost || 
                                          entry.getHost() == "www." + blockedhost;
                               }),
                hosts.end());
    

    You can accomplish a similar thing without C++0x lambdas via a struct to compare with:

    struct RemoveBlockedHost {
        RemoveBlockedHost(const std::string& s): blockedHost(s) {}
        bool operator () (const HostEntry& entry) {
            return entry.getHost() == blockedHost || entry.getHost() == "www." + blockedHost;
        }
        const std::string& blockedHost;
    };
    
    hosts.erase(std::remove_if(hosts.begin(), hosts.end(), RemoveBlockedHost(blockedhost)), hosts.end());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All source code hosting services have size limits. I am wondering that does that
All I know about the constraint is it's name ( SYS_C003415 ), but I
All too often I want a WPF slider that behaves like the System.Windows.Forms.TrackBar of
All, I currently have two projects that are under SourceSafe that I am unable
I have just tried to save a simple *.rtf file with some websites and
All, I've got the following code, and looking into ways to improve its readibility
All I need to do is have a form that does this: User enters
All, I have a question regarding Tabbed Panel in Struts2. If I have a
All the examples of Silverlight using MVVM use interface named IPropertyChanged. What is the
All I get is an error box saying: Unexpected file format. Everything was going

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.