On Windows, a wide string variable
occupies four bytes of memory which contain a pointer to a dynamically
allocated string. When a wide string variable is empty (contains a zero-length
string), the string pointer is nil and no dynamic memory is associated with
the string variable. For a nonempty string value, the string pointer points to
a dynamically allocated block of memory that contains the string value in
addition to a 32-bit length indicator. The table below shows the layout of a
wide-string memory block on Windows.
|
Offset |
Contents |
|
-4 |
32-bit length indicator (in bytes) |
|
0..Length -1 |
character string |
|
Length |
NULL character |
The string length is the number of
bytes, so it is twice the number of wide characters contained in the string.
The NULL character at the end of a wide string memory block is automatically maintained by the compiler and the built-in string handling routines. This makes it possible to typecast a wide string directly to a null-terminated string.
在Windows中,一个宽串变量占用4个字节的内存以保存动态分配串的指针。当一个宽串变量为空(包含了长度为零的串)时,串指针为nil并且没有任何动态内存与串变量相关联。对于一个非空的串值,串指针指向一个动态分配的内存块,该内存块中包含了串值以及一个32位的长度指示器。下表是Windows中宽串内存块的布局:
|
偏移量 |
内容 |
|
-4 |
32位的长度指示器(以字节计算) |
|
0..Length -1 |
字符串 |
|
Length |
NULL字符 |
串长度是字节数,因此它是包含在串中的宽字符(wide characters)的数量的两倍。
NULL字符位于宽串内存块的最后一个字节,它被编译器和内建的串处理例程自动维护。这一设计是为了将长串直接转换为空结束串。