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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:43:47+00:00 2026-06-06T22:43:47+00:00

I am taking a line of input which is separated by a space and

  • 0

I am taking a line of input which is separated by a space and trying to read the data into two integer variables.

for instance: “0 1” should give child1 == 0, child2 == 1.

The code I’m using is as follows:

int separator = input.find(' ');
const char* child1_str = input.substr(0, separator).c_str(); // Everything is as expected here.
const char* child2_str = input.substr(
    separator+1,  //Start with the next char after the separator
    input.length()-(separator+1) // And work to the end of the input string.
    ).c_str();     // But now child1_str is showing the same location in memory as child2_str!
int child1 = atoi(child1_str);
int child2 = atoi(child2_str);      // and thus are both of these getting assigned the integer '1'.
// do work

What’s happening is perplexing me to no end. I’m monitoring the sequence with the Eclipse debugger (gdb). When the function starts, child1_str and child2_str are shown to have different memory locations (as they should). After splitting the string at separator and getting the first value, child1_str holds ‘0’ as expected.

However, the next line, which assigns a value to child2_str not only assigns the correct value to child2_str, but also overwrites child1_str. I don’t even mean the character value is overwritten, I mean that the debugger shows child1_str and child2_str to share the same location in memory.

What the what?

1) Yes, I’ll be happy to listen to other suggestions to convert a string to an int — this was how I learned to do it a long time ago, and I’ve never had a problem with it, so never needed to change, however:

2) Even if there’s a better way to perform the conversion, I would still like to know what’s going on here! This is my ultimate question. So even if you come up with a better algorithm, the selected answer will be the one that helps me understand why my algorithm fails.

3) Yes, I know that std::string is C++ and const char* is standard C. atoi requires a c string. I’m tagging this as C++ because the input will absolutely be coming as a std::string from the framework I am using.

  • 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-06T22:43:51+00:00Added an answer on June 6, 2026 at 10:43 pm

    First, the superior solutions.

    In C++11 you can use the newfangled std::stoi function:

    int child1 = std::stoi(input.substr(0, separator));
    

    Failing that, you can use boost::lexical_cast:

    int child1 = boost::lexical_cast<int>(input.substr(0, separator));
    

    Now, an explanation.

    input.substr(0, separator) creates a temporary std::string object that dies at the semicolon. Calling c_str() on that temporary object gives you a pointer that is only valid as long as the temporary lives. This means that, on the next line, the pointer is already invalid. Dereferencing that pointer has undefined behaviour. Then weird things happens, as is often the case with undefined behaviour.

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

Sidebar

Related Questions

Taking this file as an example, I'm trying to read the data in a
I read a line of data in this form: KEYNAME:value,KEYNAME:value,KEYNAME:value (please note keys are
In my program I am taking user input and parsing it into a 2d
I'm taking input from textarea form and put it in the long line. Input
Working on (what should be) a simple project, taking the input from stdin and
I am taking command line arguments to main from parent to child and counting
It is taking .6-.8 seconds for this line of my code to execute in
Taking a rails tutorial, and I've run into the following problem that I'm having
How can I put <input type=text/> and <input type=button/> in one line, like this...
I've made an Applet Search Utility in which I provide a string as input

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.