   strings
  Are std::string (C++) null-terminated?   
  The result of the c_str() method is null terminated, but there is no requirement on the internal representation (for example, you can have embedded null in C++ strings). Why?   ;  Not necessarily, however, the pointer returned by the c_str function will be NULL terminated.e.g.std::string str = "test";const char* sz = str.c_str();assert( sz[0] == 't' );assert( sz[1] == 'e' );assert( sz[2] == 's' );assert( sz[3] == 't' );assert( sz[4] == '\0' );;  Then when one prints a string :string file = "file.txt";cout << file << endl;How does the above work if its not null-terminated? Is the string representedas an array of chars, internally?   ;Quote:Original post by ConcentrateHow does the above work if its not null-terminated? Is the string representedas an array of chars, internally?Internally, it's usually represented as a count + array of chars. The constructor which simply takes a char * (which is the one that would be used to convert the literal "file.txt" to an std::string) assumes that string is null-terminated, for the purpose of determining the initial length of the string.   ;Quote:Original post by ConcentrateThen when one prints a string :string file = "file.txt";cout << file << endl;How does the above work if its not null-terminated?The "file.txt" literal string is null terminated allowing std::string to work out its size. The std::string itself offers a few ways to iterate over the characters, not least begin() and end() iterators, which could be used by operator<< to stream out the character sequence.Quote:Is the string represented as an array of chars, internally?The implementation is not defined by the C++ standard, your implementation might well be different from mine. That said, there are commonly used implementations; one of which is to use a normal fixed-size array for small strings then switch to a dynamically resizing, vector-like, array for longer strings. I don't think that the standard requires std::strings to use contiguous storage though.   