0

We're seeing a strange scenario that basically boils down to the following:

std::string something = "someval";
std::stringstream s;

s << something;
std::cout << s.str();

is not equal to:

std::string something = "someval";
std::stringstream s;

s << something.c_str();
std::cout << s.str();

Taking that a step farther - the output is not gibberish in either case. What is happening is the output from case 1 appears to be mapped to another (valid) string in the system whereas the output from case 2 is what is expected.

We see this behavior by simply changing:

s << something;

To:

s << something.c_str();

I know this sounds crazy (or it does to me), and I haven't been able to replicate it out of the larger system - so sorry for no "working" example. But does anyone know how this kind of thing can happen? Can we be stepping on memory somewhere or doing something to a stringtable in some location or anything else like that?

7
  • 1
    Post the real code which shows the difference! Commented Aug 26, 2011 at 15:43
  • 4
    Does the something string in your actual code contain embedded nul characters? Commented Aug 26, 2011 at 15:43
  • 1
    If you are corrupting memory or somehow invoking undefined behaviour, then there an almost infinite number of possible causes. Probably none of them have anything to do with the stringstream class, that's just coincidence. Commented Aug 26, 2011 at 15:44
  • Yes. something or s might be ruined. Two things that have caused trouble for me: A. this is actually deleted while I'm trying to operate on members, and B. I have unwittingly linked to different variants of the standard library (debug/release, or similar mixup) and I am trying to use a std::string created by one implementation as if it were of the other implementation. Commented Aug 26, 2011 at 15:46
  • The option 2 doesn't compile in my case: s << something.c_str() leads to error C2297: '<<' : illegal, right operand has type 'const char *' (MS Visual Studio 2008, strings included by #include<string>) Commented Aug 26, 2011 at 15:47

1 Answer 1

4

It is different if the string contains nul characters, '\0'.

The .c_str() version will compute the length up to the nul, while the std::string output will know its length and output all its characters.

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

2 Comments

Thanks for the input - I'm aware of these differences though - any chance you know anything about how a literal string "Hello World" would be stored in the program memory when used to initialize a c-string as opposed to a std::string? That might help alot :)
There would be no difference there. A std::string constructor would copy from the string literal, as would a char str[] = "..". The output operators would use strlen for a char* and .size() for a std::string. That's about all the difference there is. Unless the data is corrupt somehow...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.