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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:13:03+00:00 2026-06-04T22:13:03+00:00

I have a method declared like so: /*! \brief Removes the leading and trailing

  • 0

I have a method declared like so:

/*!
\brief Removes the leading and trailing white space from a string.
\param s The string to remove the white space from.
\param white_chars Characters to be considered as whitespace.
*/
std::string Trim(const std::string &s, const std::string &white_chars = " \t\n\r");

The definition of the method is uninteresting, but here it is anyway:

std::string Trim(const std::string &s, const std::string &white_chars)
{
    size_t startidx = s.find_first_not_of(white_chars);
    if (startidx == std::string::npos) return "";
    size_t endidx = s.find_last_not_of(white_chars);
    return s.substr(startidx, endidx - startidx + 1);
}

Now in most usages of this method, I supply just the first argument. Valgrind gives me the following warning

==3338== 68 bytes in 2 blocks are possibly lost in loss record 4,639 of 7,212
==3338==    at 0x4C2B1C7: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3338==    by 0x728CA88: std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==3338==    by 0x728E2B4: char* std::string::_S_construct<char*>(char*, char*, std::allocator<char> const&, std::forward_iterator_tag) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==3338==    by 0x728E414: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::string const&, unsigned long, unsigned long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==3338==    by 0x728E441: std::string::substr(unsigned long, unsigned long) const (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==3338==    by 0x6965F0A: str::Trim(std::string const&, std::string const&) (appbase.cpp:90)
==3338==    by 0x5F481D7: netopt::Element::set_ID(std::string const&) (netopt_elem.cpp:85)

Note “blocks are possibly lost”? I’ve seen memory leaks where Valgrind tells me “blocks are definitely lost”, but this is a less certain message.

So the question becomes, am I causing a memory leak by assigning a default value to an std::string &? If so, what am I doing wrong?

  • 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-06-04T22:13:05+00:00Added an answer on June 4, 2026 at 10:13 pm

    There is no technical issue, but philosophically creating a temporary std::string at each call is not so nice. Especially with libstdc++ (which you appear to use) since it causes a memory allocation each time (lack of Short String Optimization).

    Since find_first_not_of has an overload taking a char const*, it would be better to provide two overloads instead:

    // First, the one with the default argument, but without a temporary
    std::string Trim(std::string const& s, char const* white_chars = " \t\n\r");
    
    // Second, for convenience, one with a `std::string`
    std::string Trim(std::string const& s, std::string const& white_chars);
    

    This also means that when calling Trim(xx, "abc") you will avoid the temporary std::string being generated 🙂

    Of course, the overkill solution is to reuse already written code: Boost String Algorithm has many algorithms for string manipulation, including trim.

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

Sidebar

Related Questions

I have an abstract method in a base class declared like so... public abstract
I have a method declared on an interface like so: public interface IInterface {
I have a class named Entry declared like this: class Entry{ string Id {get;set;}
If I have a method declared like this: private void someFunction(object[] param1) When I
Say I have an array of Strings declared like this: String[] strings = new
I have a RESTful WCF service with a method declared like this: [OperationContract(Name =
I would like to have a sealed trait which have a declared method that
I have declared a method where it will dynamically create a database query string
I have a method that was declared like this: public ButtonObject CreateStandardButton(type1 arg1, type2
I have the following template method declared in my interface: class IObjectFactory { public:

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.