1

The following code prints the expected output within the for loop , but once I exit the loop , it only prints "Here" , with nothing underneath it. What am I missing here?

char ** strs = new char*[n];
for (int i = 0; i < n; i++)
{
    string str;
    getline(cin, str);
    strs[i] = const_cast<char*>(str.c_str());
    cout << strs[i] << endl;
}
cout  <<"here" <<strs[1] << endl;
Ahmed Elyamani
  • 486
  • 2
  • 10

2 Answers2

6

Your pointers are dangling.

You've stored pointers to the data buffer of a std::string. That buffer ceases to exist on each new iteration, to be replaced by the buffer of the next iteration's version of str; furthermore, there is absolutely no trace of it after the loop ends.

Don't do this. Just store std::strings.

That const_cast is completely ill-advised too. Not sure why you're doing it.

Lightness Races in Orbit
  • 358,771
  • 68
  • 593
  • 989
  • I'm trying to read an input string then cast it into an array of chars to call some other function. That const_cast was what I found here on SO – Ahmed Elyamani Aug 24 '15 at 22:31
  • @AhmedElyamani Store the strings and call the other function with str.c_str(). You will spend less time debugging. If you need to call the other function with a char *, either change the other function to take string & or const char * or copy str.c_str to your own buffer that you can safely pass to the other function. Don't cast const to non-const. You get all sorts of crashes and weird errors when you attempt to write into constant data. – user4581301 Aug 24 '15 at 22:48
  • @AhmedElyamani: Please point us to the answer that advised such a `const_cast`, so that we may downvote it and prevent anyone else from falling into the trap. – Lightness Races in Orbit Aug 25 '15 at 09:28
  • @LightnessRacesinOrbit It was submitted as part of this question:http://stackoverflow.com/questions/13294067/how-to-convert-string-to-char-array-in-c Then again , I'm only a few days into c++. I probably misinterpreted something there. – Ahmed Elyamani Aug 25 '15 at 14:55
  • 1
    @AhmedElyamani: It's not generally advisable to copy/paste code from _questions_, when the question was posted for the express reason that **the code didn't work** (and, yes, that code there is awful!). Take inspiration from _answers_, instead, and even then only when you understand them and know _why_ you're absorbing the solution into your own code. – Lightness Races in Orbit Aug 25 '15 at 14:58
  • @LightnessRacesinOrbit You are right. It's just that coming from javascript I was trying to solve a specific problem in c++ and all these type castings were the last of my worries , so I rushed to copying what seemed to be a straightforward approach , but turned out not to be. – Ahmed Elyamani Aug 25 '15 at 15:02
4

What am I missing here?

In theory, you are seeing the symptoms of undefined behavior.

You are storing the a pointer to the data held by str in strs[i] but str is deleted when you get out of the for loop. The pointer is not valid after the end of the loop.

In practice, you are seeing only the last value read since every element of strs stores the same pointer value.

You can avoid problems by using standard containers.

std::vector<std::string>> strs;
for (int i = 0; i < n; i++)
{
    string str;
    getline(cin, str);
    strs.push_back(str);
    cout << strs[i] << endl;
}
R Sahu
  • 196,807
  • 13
  • 136
  • 247