Originale-mail to me for new edition

 

Long strings

 

AnsiString, also called a long string, represents a dynamically allocated string whose maximum length is limited only by available memory. It uses 8-bit ANSI characters.

A long-string variable is a pointer occupying four bytes of memory. When the variable is empty, that is, when it contains a zero-length string the pointer is nil and the string uses no additional storage. When the variable is nonempty, it points to a dynamically allocated block of memory that contains the string value, a 32-bit length indicator, and a 32-bit reference count. This memory is allocated on the heap, but its management is entirely automatic and requires no user code.

Because long-string variables are pointers, two or more of them can reference the same value without consuming additional memory. The compiler exploits this to conserve resources and execute assignments faster. Whenever a long-string variable is destroyed or assigned a new value, the reference count of the old string (the variable’s previous value) is decremented and the reference count of the new value (if there is one) is incremented; if the reference count of a string reaches zero, its memory is deallocated. This process is called reference-counting. When indexing is used to change the value of a single character in a string, a copy of the string is made if but only if its reference count is greater than one. This is called copy-on-write semantics.

 

Topic groups

 

See also

About string types

 

 

译文

 

长串

 

AnsiString也叫做长串(long string),用来表示动态分配的串,其最大长度仅受限于可用内存的大小。长串使用8位的标准(ANSI)字符。

长串变量是一个占用4个字节内存的指针。当串变量为空时(此时的串即为空串),它包含了一个零长度的串,指针此时为空指针nil,并且该串不再使用额外的存储空间。当串变量不为空时,它指向一个动态分配的内存块,该内存块中包含串值、一个32位的长度指示和一个32位的引用计数;该内存块分配于堆(heap)中,其管理完全自动且不需要任何用户代码。

因为长串变量是指针,所以两个或更多的长串变量可以引用相同的串值而不用消耗额外的内存。编译器使用这一特点来保存资源并执行更快的赋值。只要长串变量被销毁或赋予新的串值,那么旧串(先前的值)的引用计数递减并且新串(当前值)的引用计数递增;如果一个串的引用计数达到零,那么它占用的内存被释放。该处理叫做引用计数(reference-counting)。当通过对串索来改变串中单个字符的值时,当且仅当该串的引用计数大于1时生成该串的副本。该处理叫做写复制(copy-on-write)。

 

主题组

 

相关主题

关于串类型