3

Yes, I did check other threads and I have come to a conclusion. I just want you to confirm it so that I don't have any misconceptions.

Java String objects are not null terminated.

C++ std::string objects are also not null terminated

C strings or C-style strings if you will (array of characters), are the only strings that are null-terminated.

Correct or Incorrect?

Community
  • 1
  • 1
B-Mac
  • 557
  • 2
  • 14

7 Answers7

5

C-strings are 0-terminated strings. You aren't forced to use them in C though.

Both C++ std::string and Java strings are counted strings, which means they store their length.

But C++ std::strings are also followed by a 0 since C++11, making them 0-terminated if (as often the case) they don't contain any embeddded 0, for better interoperability with 0-terminated-string APIs.

Community
  • 1
  • 1
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
2

All of those are in themselves correct, but petty pedantery: C-style strings are not unique to C, there are other places where such things occur (most commonly in various forms of Assembler code, and C being a language originally designed to be "slightly above assembler" makes this "no surprise").

And in C++11, std::string is guaranteed to have a NUL terminator after the last actual string character [but it's valid to store NULL characters inside the string if you wish] (at least if you call c_str(), but in the implementations I've looked at, it's stored there on creation/update)

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
1

All the statements are not wrong, but need to clarify more of the specifics in each of the mentioned languages.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
1

That is correct c++ std::string and java String both hold private fields indicating the length of the string. A NULL terminator is not needed.

The std::string method c_str returns the string as a NULL terminated char array for use when a NULL terminator is required e.g. c string functions such as strlen.

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
0

Almost correct.

  • C-string are not just an array of characters. They are a null-terminated array of characters.

    So if you have an array of characters, it's not a C-string yet, it's just an ordinary array of characters. It has to have a terminating null character to be a valid C-style string.

  • Additionally, an std::string must also be null-terminated (since C++11). (But it still has a private variable holding the length of the string.)

Community
  • 1
  • 1
Emil Laine
  • 41,598
  • 9
  • 101
  • 157
0

I don't know about the Java part, but in C++11 std::strings are NUL-terminated (besides storing the chars count), i.e. &s[0] returns the same string as s.c_str() (which is NUL-terminated, as a raw C-style string).

See this answer for more details.

Community
  • 1
  • 1
Mr.C64
  • 41,637
  • 14
  • 86
  • 162
  • So, is the null termination character stored at index 0 instead of the last index in c++ 11? Can you please elaborate buddy? We're on to something very interesting here. :) – B-Mac Feb 24 '15 at 22:33
  • @Grendan: Seems like trying to include the reasoning there. – Deduplicator Feb 24 '15 at 22:34
  • @Grendan: &s[0] is a pointer to the beginning of the string wrapped in the std::string class. If this pointer points to the same string returned by c_str(), since c_str() returns a NUL-terminated string by definition (i.e. a NUL-terminated C-style string), then this implies that also the string returned by &s[0] is NUL-terminated. The NUL-terminator is at the END of the string. Note also that since std::strings keep the character count, you can also have more than one single NUL char embedded in the string. – Mr.C64 Feb 24 '15 at 22:40
0

The question you need to be asking is why C-String should be null terminated.

The answer is the string manipulation functions needs to know the exact length of the string. As strings in C are just array of characters there is no information that tells (this is the size of this array) they need something to help determining the size of array which is the null character standing at the end of it.

Where as in Java strings are instances of the String class which has the length field so there is no need for the null termination.

The same thing apply to strings in c++.

rullof
  • 7,124
  • 6
  • 27
  • 36