-1

So... when I go:

cout<<stringName<<endl;

I get:

NT

But when I go:

cout<<stringName.c_str()<<endl;

I get:

NTNT

Why?

5
  • Are you using wstring by any chance. Feb 2, 2009 at 23:45
  • Don't even know what that is.
    – Alex
    Feb 2, 2009 at 23:46
  • Are you doing any fork()ing in the vicinity -- maybe you have multiple processes flushing the more-or-less same buffers. Feb 3, 2009 at 0:04
  • There is some other error in your code. Feb 3, 2009 at 1:55
  • 1
    @windfinder, please report back. What did it turn out to be? Have you tracked down the bug? Feb 4, 2009 at 18:15

4 Answers 4

9

A quick test with the following code:

#include <string>
#include <iostream>

using namespace std;

int main(void) {
    string str = "NT";
    cout << str.c_str() << endl;
    return 0;
}

produces one instance of NT so it looks like you probably have another output call somewhere.

4
  • How could that be the cause? Assuming he has changed just this one line from 'stringName' to 'stringName.c_str()', that can't change the number of times the string is printed. Plus, he prints a newline, so it's clearly not two instances of this code being run. Feb 3, 2009 at 18:48
  • @Larry There is no indication of what the rest of the code looks like or that the same input was used between runs so it could be in a conditional somewhere. There is also no guarantee that a previous call would include an endl either. I said 'probably' for a reason. Feb 3, 2009 at 19:06
  • @Kevin If there is "another output call" it happens first and doesn't have a newline. I was taking at face value that the only thing he changed was that one line, not the "other". Either way, the CHANGE in behavior as described is not accounted for merely by presence of another "NT" print command. Feb 4, 2009 at 18:20
  • @Larry I agree with you that taking this at face value possible hints at a more difficult to diagnose problem, but my first instinct in a situation like this is to look for the simplest most obvious solution first; especially when given limited details and no context. Feb 5, 2009 at 8:09
4

A traditional C string (accessed through a char const*) has a sequence of characters terminated by a character 0. (Not the numeral 0, but an actual zero value, which we write as '\0'.) There's no explicit length — so various string operations just read one character at a time until it hits the '\0'.

A C++ std::string has an explicit length in its structure.

Is it possible that the memory layout of your string's characters looks like this:

'NTNT\0'

but the string's length is set to 2?

That would result in exactly this behavior — manipulating the std::string directly will act like it's just two characters long, but if you do traditional C operations using s.c_str(), it will look like "NTNT".

I'm not sure what shenanigans would get you into this state, but it would certainly match the symptoms.

One way you could get into this state would be to actually write to the string's characters, something like: strcat((char *)s.c_str(), "NT")

5
  • You would get that behaviour only if std::string::c_str() is faulty. c_str() is guaranteed by the standard (21.3.6[1]) to point to an array (not necessarily the same storage as string uses internally) terminated by a \0 Feb 3, 2009 at 19:01
  • @Steve, not if c_str() doesn't do anything and the data is already maintained in a null terminated way. Then if you overwrite that null terminator by directly manipulating it with data(), it could have this result. Feb 3, 2009 at 19:16
  • And in general, if you've done anything invalid to the string object and the result is that c_str() doesn't do what you expect, that doesn't mean c_str() is faulty ;-) Feb 3, 2009 at 19:52
  • A C-string is not a char*. A C-string is a char[N], possibly const, and you generally pass around pointers to it. Aug 8, 2011 at 14:05
  • 1
    -1: This answer can be summed up as "You're getting "NTNT". It is possible that something went wrong, such that you're getting "NTNT". That would match the symptoms." Feb 24, 2012 at 20:10
2

Show more code. It seems like you did cout << ealier and forgot that you did it. What does it print if you do cout<< "mofo" << stringName.c_str()<< "|||" << endl; Does it say NTmofoNT||| ? if so that may well be what happened ;)

0
1

This is not a problem with c_str(), but probably related to some other anomaly in the rest of the program.

Make a "hello world" application that does these same operations and you'll see it works fine there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.