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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:08:15+00:00 2026-06-12T00:08:15+00:00

I have the code below: std::string myName = BLABLABLA; //check if there are illegal

  • 0

I have the code below:

std::string myName = "BLABLABLA";

//check if there are illegal characters
for (unsigned int i = 0; i < myName.length(); i++)
{
    const char& c = myName[i];
    if (!(isalnum(c) || (c == '_') || (c == '-')))
    {
        return 0;
    }      

}

This is the output of valgrind at line “const char& c = myName[i];”

==17249== 51 bytes in 1 blocks are possibly lost in loss record 116 of 224
==17249==    at 0x4C2714E: operator new(unsigned long) (vg_replace_malloc.c:261)
==17249==    by 0x602A498: std::string::_Rep::_S_create(unsigned long, unsigned long,       
std::allocator<char> const&) (in /usr/lib64/libstdc++.so.6.0.16)
==17249==    by 0x602A689: std::string::_M_mutate(unsigned long, unsigned long,   
unsigned long) (in /usr/lib64/libstdc++.so.6.0.16)
==17249==    by 0x602AFB5: std::string::_M_leak_hard() (in 
/usr/lib64/libstdc++.so.6.0.16)
==17249==    by 0x602B0A4: std::string::operator[](unsigned long) (in /
/usr/lib64/libstdc++.so.6.0.16)

I do not see anything wrong with this…

  • 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-12T00:08:16+00:00Added an answer on June 12, 2026 at 12:08 am

    Yes, it’s the horrible COW implementation!
    You can also force use of the const (and therefore non-mutating) overloads like so:

    std::string const myName = "BLABLABLA";
    
    //check if there are illegal characters
    for (unsigned int i = 0; i < myName.length(); i++)
    {
        const char& c = myName[i];
        if (!(isalnum(c) || (c == '_') || (c == '-')))
        {
            return 0;
        }      
    }
    

    or (if you don’t want to modify the original string type):

    std::string myName = "BLABLABLA";
    std::string const &cref = myName;
    //check if there are illegal characters
    for (unsigned int i = 0; i < myName.length(); i++)
    {
        const char& c = cref[i];
        if (!(isalnum(c) || (c == '_') || (c == '-')))
        {
            return 0;
        }      
    }
    

    etc.


    COW reference, because I knew I’d written something about it somewhere.

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

Sidebar

Related Questions

i have written a sample program.below: #include<iostream> #include<string> #include<set> using namespace std; int main()
I have code as below: var s : String = hello world var xml
I have the code below. void *timer1_function(void * eit); pthread_t timer1; int thread_check1 =
I have the code below : public class Anything { public int Data {
In the sample code below std::string result = exec( dir ) ; cout<<result; I
I have the following code: #include <iostream> #include <string> void main() { std::string str;
I have the code below: // IBase.h #include <iostream> class IBase{ public: virtual string
I have code below: <select id=testSelect> <option value=1>One</option> <option value=2>Two</option> </select> <asp:Button ID=btnTest runat=server
I have code that looks more or less like the code below but it
I have the code below. It looks long and complicated and now I have

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.