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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:36:26+00:00 2026-06-18T02:36:26+00:00

I am having some valgrind issues when I write code like this: static std::string

  • 0

I am having some valgrind issues when I write code like this:

static std::string function(std::string test)
{
        size_t pos = test.find(',');
        if (pos == test.npos)
        {
            // no comma
            //
            pos = test.length();
        }

        return test.substr(0, pos); //Valgrind is reporting possibly lost bytes here

}

Now my question is should I do this instead?

static std::string function(std::string test)
{
        size_t pos = test.find(',');
        if (pos == test.npos)
        {
            // no comma
            //
            pos = test.length();
        }
        static std::string temp = test.substr(0, pos);
        return temp;

}

I think that having the string temp static is sort of important because function is static so anything that is returned by function should have the same lifetime as the object incapsulating function. Or is my analysis flawed?

Thank you

  • 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-18T02:36:27+00:00Added an answer on June 18, 2026 at 2:36 am

    Now my question is should I do this instead?

    No, definitely not. No need to create such a temporary (and espcially not a static one, making the function non-reentrant, or in the end only working the first time in your case). Valgrind is reporting rubbish here, I guess. Your code looks fine like it is.

    Or is my analysis flawed?

    Yes, it is. A static class method doesn’t have an object associated with it, which could have a distinct lifetime, anyway (it’s like a normal non-class function, just that you need to scope it with the class name when calling). Other than that you don’t return a reference to any temporary string anyway, but a new object copied from temp, so there are no lifetime problems to be taken into account here.

    Other than that, you might want to consider taking the test object by const-reference, since you don’t modify nor copy it (could this be related to general misunderstandings about object identity and object value?).

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

Sidebar

Related Questions

Having some issues running my code local. I have host file setup like this:
Having some issues with C. I have this is my code: // First line
Having some pain with this event, I have some code like this for a
Having some issues while variable referencing inside foreach loop. This is my CakePHP code
Having some trouble with this code. Trying to return a tuple of tuples (coordinates)
I'm having some issues where when if I run this C extension outside of
Having some issues with one small function I'm working on for a homework assignment.
Having some issues with this search engine I am trying to put together. I'll
Having some trouble with this.. <?php EG204_ExoSkel(); function EG204_ExoSkel() { $to_be_end = 'Red'; everything_loop();
Having some issues deploying this. I've tried to deploy it twice now. Here's what

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.