Quick start manual
11-6
Delphi Language Guide
Internal data formats
Long string types
A long string variable occupies four bytes of memory which contain a pointer to a
dynamically allocated string. When a long 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 and a 32-bit reference count. The table below shows the layout
of a long-string memory block.
The NULL character at the end of a long string memory block is automatically
maintained by the compiler and the built-in string handling routines. This makes it
possible to typecast a long string directly to a null-terminated string.
For string constants and literals, the compiler generates a memory block with the
same layout as a dynamically allocated string, but with a reference count of –1. When
a long string variable is assigned a string constant, the string pointer is assigned the
address of the memory block generated for the string constant. The built-in string
handling routines know not to attempt to modify blocks that have a reference count
of –1.
Wide string types (Windows)
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.
The string length is the number of bytes, so it is twice the number of wide characters
contained in the string.
Table 11.1 Long string dynamic memory layout
Offset Contents
–8 32-bit reference-count
–4 length in bytes
0..Length – 1 character string
Length NULL character
Table 11.2 Wide string dynamic memory layout (Windows)
Offset Contents
–4 32-bit length indicator (in bytes)
0..Length – 1 character string
Length NULL character