Why is a null terminator necessary?
Asked Answered
S

4

5

I've been teaching myself c++ for the last couple days to prepare for my freshman year as a CS major. I'm on C-style strings right now, and wondering what the point of a null terminator is.

I understand that it's necessary, but I guess I just don't fundamentally understand why a string wouldn't just end on its last char.

Solenne answered 28/6, 2013 at 18:15 Comment(2)
How do you know what the last character is?Gerdy
You need an agreed upon last char, in C it's a 0 byte. In ASM, it's usually a $ char. You know the last char, but how can the compiler know what you're thinking?Heisenberg
I
18

I just don't fundamentally understand why a string wouldn't just end on its last char.

There are several ways of knowing where is the "last char":

  1. Store the number of characters in the string separately from the string's characters,
  2. Put a marker that indicates the last char of the string, or
  3. Store the pointer to the last char of the string separately from the string's characters.

C choose the second route; other languages (Pascal, etc.) choose the first route. Some implementations of C++ std::string choose the third route* .


* Even std::string implementations that use the first or the third approach null-terminate their buffers for compatibility with the C portions of the library. This is necessary to ensure that c_str() returns a valid C string.
Imbrue answered 28/6, 2013 at 18:18 Comment(3)
4. Make all strings fixed size arrays, possibly padded with spaces. But this insanity has only to be endured in fortran.Caesar
I think it should be mentioned that even if C++ string generally uses the first solution (or the third) for efficiency reasons so that it doesn't have to recalculate the string length every time it is needed, it still uses the second solution too to keep the string compatible with C APIs (assuming the string doesn't contain null characters, which are legal with C++ string but shorten strlen compared to string::size()). -- At the very least when calling string::c_str() (even though most implementations probably only write the null terminator once when the string's size changes).Mauchi
@Mauchi That's a great comment, I edited the answer to include it. Thanks!Imbrue
S
2

In C and C++, c-strings are stored in a character array. To allow strings of different lengths, these arrays are often allocated much larger than the actual strings they are to contain. For example, a programmer may allocate a char[256] array, which can hold a string with a length anywhere between 0 characters and 255. But the computer has to be able to know exactly how long the string actually is, so it must end with a null character. Otherwise, it would be neccessary for the character array length to be exactly the same as the string (an impractical solution, as allocating and copying memory uses a lot of resources).

Scalf answered 28/6, 2013 at 18:21 Comment(0)
L
0

Because a c-style string doesn't know what character is the last character. For example if you are reading a name you might make a buffer like so:

char buf[256] // this allows c-style strings that contain 255 characters

But when you go to populate that buffer you may not (likely won't) use all of the space. If you fill it with "Jack", the only information you care about is the first five indices, not all 256.

Leghorn answered 28/6, 2013 at 18:20 Comment(0)
O
0

Consider each character of the string as memory blocks in the memory. If a string is placed in memory. After that a another string is placed adjacent to it , then the computer will think that the 2nd string is joined to 1st ,if null is absent. So, null acts as delimitor

Oocyte answered 28/6, 2013 at 18:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.