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 7527741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:10:49+00:00 2026-05-30T04:10:49+00:00

I ran into this issue on accident while debugging a faulty string trim function

  • 0

I ran into this issue on accident while debugging a faulty string trim function and now I’m wondering what exactly is causing this behavior.

Here’s a code snippet with just the relevant bits:

#include <iostream>
#include <string>
using namespace std;

void debug(string s) { cout << "Len: " << s.length() << "; Value: '" << s << "'" << endl; }

int main()
{
    string str = "ABC";
    debug(str);

    str.erase( str.begin() + 4, str.end() );  // Magic!
    debug(str):

    return 0;
}

You’ll notice that the erase function is taking an iterator that starts just beyond the end of the string. This isn’t the exact input used when I ran into the issue (it was having a +1 which broke empty strings), but it has a similar effect. Here’s the output:

Len: 3; Value: 'ABC'
Len: 4; Value: 'ABC '

When I change the str.begin() + 4 to something greater like + 10 it becomes clear that I’m starting to pull in garbage data.

My question is: how is it that using the erase() function ends up making the string longer than before? Also, is it just luck, or is the next character after the end of the string always whitespace?

  • 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-30T04:10:50+00:00Added an answer on May 30, 2026 at 4:10 am

    string::erase() has the following precondition:

    • Requires: first and last are valid iterators on *this, defining a range [first,last)

    since your code doesn’t hold to this, it’s exhibiting undefined behavior. There’s not a whole lot of point in analyzing why it breaks something – basically the erase() function is making an assumption that’s not true.

    But if you’re really curious about exactly what’s going on you would at least need to specify which tool chain you’re using (since UB varies quite a bit depending on the compiler and library). It would probably be easiest to just step through the code in a debugger to see why it’s behaving this way in this instance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This morning I ran into an issue with returning back a text string as
Just about everyone has ran into this specific issue: function addLinks () { for
I ran into this issue twice now, and am curious to know if there
I ran into this today when unit testing a generic dictionary. System.Collections.Generic.Dictionary<int, string> actual,
Never ran into this problem with jQuery before. I have the following: $(document).ready(function() {
I ran into this issue when trying to add a CMFCMenuButton to an existing
Has anyone ran into this issue? We have a mobile site that can't use
I'm trying to create a website for myself and just ran into this issue:
I was helping someone with their homework and ran into this strange issue. The
Edit: it's been ages since I've ran into this "issue" back then, and by

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.