0

Can anyone explain how the following code is working and does not crash the application?

int main() {
    char *tempStr = new char[5];
    tempStr[0] = '\0';
    string stemp = "helloworld";
    stringstream sstream;
    sstream.str(stemp);
    cout << "len before = " << strlen(tempStr);
    sstream >> tempStr;
    cout << "len after = " << strlen(tempStr) << endl;
    cout << tempStr << endl;
    delete[] tempStr;
    return 1;
}

I am getting the output as

len before = 0
len after = 10
helloworld
  1. Did stringstream allocate memory for the extra characters in the char pointer?
  2. Also want to know the correct way to copy data from stringstream to char* array, without exceeding the memory allocated for char*?
David G
  • 94,763
  • 41
  • 167
  • 253
N3Xg3N
  • 97
  • 1
  • 12

2 Answers2

3

Did stringstream allocate memory for the extra characters in the char pointer?

No. Your code invokes undefined behavior.

Also want to know the correct way to copy data from stringstream to char* array, without exceeding the memory allocated for char*?

It is not a good idea to read into char*. Use std::string to read input from stream. But then if you still want to know for the sake of knowledge, use std::istream::read().

if ( sstream.read(tempStr, 5 ) )
{
   //read succeeded
}

By the way, you can merge these two lines:

stringstream sstream;
sstream.str(stemp);

into one:

stringstream sstream(stemp);

or simply this:

stringstream sstream("helloworld"); //no need of stemp!

Hope that helps.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • Can we also do `sstream << stemp`? – David G Mar 28 '13 at 13:03
  • Yes, since it is `stringstream` which is both for input and output. – Nawaz Mar 28 '13 at 13:04
  • i have a requirement, cant use string. so the only way is use `istream::read` and specify the max length of your char array. Thanks. – N3Xg3N Mar 28 '13 at 13:05
  • actually i was reading both `integer` and `char*` datatypes with `>> operator` in a `macro`. Is there a way to still do that without doing differently for `integer` and `char*`. – N3Xg3N Mar 29 '13 at 12:21
  • @N3Xg3N: I didn't understand what you're saying? What is `macro` here? Post your code at ideone.com or pastebin.com – Nawaz Mar 29 '13 at 13:04
  • `#define INPUT_STRUCT_DATA(data,strct_mbr)\ {stringstream inp(data); \ inp >> strct_mbr; \ inp.str(string()); \ inp.clear();}` i am passing a string in the first param and a struct member in the second, can be int or char* – N3Xg3N Mar 30 '13 at 09:19
  • @N3Xg3N: Why don't you experiment with them writing some small programs? That would also help you understand things better. – Nawaz Mar 30 '13 at 10:06
  • of course tried writing snippets of code, was trying to achieve reflection by filling in struct data using stringstream `operator >>`. may not be a simple way – N3Xg3N Apr 01 '13 at 10:01
1
  1. No. You overwrote memory, invoking undefined behavior, but nothing obvious happened, so the error went unnoticed. There is no requirement that doing something like this should trigger any kind of human-visible error or special action, thus the wording undefined behavior.
  2. You're going to have to do it chunk by chunk, and re-allocate the char array if it runs out of space. In C++, there's little point in doing this manually. Just use std::string and be done.
unwind
  • 391,730
  • 64
  • 469
  • 606