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; 
3
  • Ahmed could you please explain the thought processes that led to your design decisions here? Commented Aug 24, 2015 at 22:21
  • This is only a piece of the code. throughout the code I call a function that requires passing a char array , I didn't want to use the casting multiple times. Commented Aug 24, 2015 at 22:23
  • @AhmedElyamani: Please explain the thought processes that led to your design decisions here. Commented Aug 25, 2015 at 9:29

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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
@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.
@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.
@LightnessRacesinOrbit It was submitted as part of this question:stackoverflow.com/questions/13294067/… Then again , I'm only a few days into c++. I probably misinterpreted something there.
@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.
|
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; } 

1 Comment

It's not the address of str.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.