B-Mac
B-Mac

Reputation: 567

Null termination in strings

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?

Upvotes: 3

Views: 1280

Answers (7)

Paul Rooney
Paul Rooney

Reputation: 21609

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.

Upvotes: 1

Deduplicator
Deduplicator

Reputation: 45654

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.

Upvotes: 5

Emil Laine
Emil Laine

Reputation: 42828

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.)

Upvotes: 0

rullof
rullof

Reputation: 7424

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++.

Upvotes: 0

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

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

Upvotes: 1

Mr.C64
Mr.C64

Reputation: 42924

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.

Upvotes: 0

Mats Petersson
Mats Petersson

Reputation: 129314

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)

Upvotes: 2

Related Questions