-->

Why doesn't the null-terminator placed arbitra

2019-04-25 14:43发布

问题:

The null character or null-terminator (\0) is to be used to terminate a contiguous sequence of characters. I find that in C, I can add the character into a string at a random position and the string will be cut off from that point. For example:

char * s = "Hello\0World";

will result in s being equal to the string "Hello". In JavaScript, however, this is not the case:

var s = "Hello\0World";

The above won't work as expected. s will be equal to the string "HelloWorld".

Why doesn't this work?

回答1:

JavaScript does not use NULL terminated strings, while C does.

Javascript strings are stored by keeping track of the characters and the length separately instead of trying to assume that a NULL marks the end of the string.



回答2:

The C string still points to an address in memory where "Hello\0World" is stored, only that most string handling functions considers 0 end of string. For some functions you must pass a string length argument, but most simply read until they find the null byte. In memory the string is actually "Hello\0World\0".

A JavaScript engine cannot determine string length by looking for a null byte, since you in such a case wouldn't ever be able to have a nullbyte inside a string. There's probably something about that in the specs. The engine must instead storing the length of the string separately, and then read that many characters from memory whenever you access the string.

And how to properly parse and store the size of buffers is something scripting languages usually try to hide from the user. That's half the purpose of scripting, to not require the programmer to worry about adding 0 to created character buffers and or storing string length separately so that string handling functions don't print a bunch of random characters outside your buffer looking for a nullbyte...

So exactly how does a JavaScript string behave? I don't know, it's probably up to the engine to describe its properties in depth. As long as you interface with the object like the specification says, it can be implemented in whatever manner, using structs for buffer and length, using a translation character for 0, using a linked list of characters, etc...



回答3:

In Javascript a NULL byte in a String is simply a NULL byte in a string. If you want truncate the string

var s = "Hello\0World".split("\0").shift();

but in this case I think it not need to disturb the null byte :)