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 question.(5)

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

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/How can i handle -basic_string::_m_construct null not valid?
Next
Answered
Adam Boulanger
  • 34
Adam Boulanger
Asked: May 18, 20222022-05-18T10:32:51+00:00 2022-05-18T10:32:51+00:00In: cpp

How can i handle -basic_string::_m_construct null not valid?

  • 34

. Advertisement .

..3..

. Advertisement .

..4..

I am working on cpp, but I found the following warning message:

terminate called after throwing an instance of 'std::logic_error'
  what():  basic_string::_M_construct null not valid
Aborted (core dumped)

Is there any way to stabilize the issue “basic_string::_m_construct null not valid”? I read a lot of topics about this, but all of them were trying to install anything. Is this the correct way, or any recommendation for me? Please find the beginning command below:

std::string myfunc(std::string input){
  std::string b="";
 
  if (!input.size()) return 0;
  for (int i = 0; i < input.size(); i++){
 
  if ( input[i] < 'a' || input[i] > 'z'|| isalpha(input[i]) || isupper(input[i]) ) return 0;
  }
  b = input;
  //just copy the input string for now.
  return b;
 }
int main(){
  std::string input="Somthing";
  std::cout << myfunc(input)<< std::endl;
  return 0;
 }
construct null not valid
  • 2 2 Answers
  • 81 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    lyytutoria Expert
    2022-06-07T03:35:42+00:00Added an answer on June 7, 2022 at 3:35 am

    The cause:

    There are two return 0; statements in your function. The function will return a std::string but there is no constructors get a int input. There is a constructor which can admit a const char * reference which 0 is changed implicitly. However, building a std::string with a null char * pointer is not a defined behavior. You don’t catch an exception std::logic_error which your implementation has selected in your code. Therefore, the error appears.

    Solution:

    To solve this problem effectively, you need to returan an empty string:

    std::string myfunc(const std::string &input){
    if (input.empty()) return "";
    for (int i = 0; i < input.size(); ++i){
    char ch = input[i];
    if ( !((ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')) ) return "";
    }
    return input;
    }

    Then the caller will check whether the return value has been set to empty or not:

    if (myfunc(input).empty())
    //  incorrect, let's do something 
    else
    // correct, let's do something else

    This function returns a bool rather than a std::string, which would help you better.

    bool isvalid(const std::string &input){
    if (input.empty()) return false;
    for (int i = 0; i < input.size(); ++i){
    char ch = input[i];
    if ( !((ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')) ) return false;
    }
    return true;
    }
    
    // if you still need this function for something...
    std::string myfunc(const std::string &input){
    if (!isvalid(input)) return "";
    return input;
    }
    
    if (!isvalid(input))
    // incorrect, let's do something
    else
    // correct, let's do something else
    • 17
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Arthur Gimenez
    2022-05-25T20:15:19+00:00Added an answer on May 25, 2022 at 8:15 pm

    You can return false or true if you change the return type to bool

    bool myfunc(std::string input) {
    ^^^^

    If you want to return false, then you should do so.

    if (!input.size()) return false;
     ^^^^^

    It is not an error to return 0 from a Boolean Function. However, 0 will be automatically converted to false. But it is stylistically more important to clarify what you mean.

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

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

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.