What are the problems of a zero-terminated string that length-prefixed strings overcome?

后端 未结 6 1773
滥情空心
滥情空心 2021-02-06 21:57

What are the problems of a zero-terminated string that length-prefixed strings overcome?

I was reading the book Write Great Code vol. 1 and I had that question in mind.<

6条回答
  •  灰色年华
    2021-02-06 22:32

    It is best summarized in The Most Expensive One-byte Mistake by Poul-Henning Kamp.

    1. Performance Costs: It is cheaper to manipulate memory in chunks, which cannot be done if you're always having to look for the NULL character. In other words if you know before hand you have a 129 character string, it would likely be more efficient to manipulate it in sections of 64, 64, and 1 bytes, instead of character by character.
    2. Security: Marco A. already hit this pretty hard. Over and under-running string buffers is still a major route for attacks by hackers.

    3. Compiler Development Costs: Big costs are associated with optimizing compilers for null terminating strings that would have been easier with the address and length format.

    4. Hardware Development Costs: Hardware development costs are also large for string specific instructions associated with null terminating strings.

提交回复
热议问题