8.2.7 String types

Ansistring types

The ansistring is a dynamically allocated string which has no length limitation (other than addressable memory). When the string is no longer being referenced (its reference count reaches zero), its memory is automatically freed.

If the ansistring is a constant, then its reference count will be equal to -1, indicating that it should never be freed. The structure in memory for an ansistring is shown in table (8.3) for 32-bit programs, and table (8.4) for 64-bit programs


Table 8.3: AnsiString memory structure (32-bit model)

OffsetContains


-12Code page indicator (2 bytes).
-10Character size (2 bytes)
-8Longint with reference count.
-4Longint with actual string size.
0Actual array of char, null-terminated.




Table 8.4: AnsiString memory structure (64-bit model)

OffsetContains


-24Code page indicator (2 bytes).
-22Character size (2 bytes)
-16Sizeint with reference count.
-8Sizeint with actual string size.
0Actual array of char, null-terminated.



Shortstring types

A shortstring occupies as many bytes as its maximum length plus one. The first byte contains the current dynamic length of the string. The following bytes contain the actual characters (of type char) of the string. The maximum size of a short string is the length byte followed by 255 characters.

Unicodestring types

A unicode string is allocated on the heap, much like an ansistring. Unlike the ansistring, a unicodestring takes 2 bytes per character, and is terminated with a double null. It is reference counted just like an ansistring. The structure in memory for a unicodestring is shown in table (8.3) for 32-bit programs, and table (8.4) for 64-bit programs


Table 8.5: UnicodeString memory structure (32-bit model)

OffsetContains


-8Longint with reference count.
-4Longint with actual string size.
0Actual array of char, null-terminated.




Table 8.6: UnicodeString memory structure (64-bit model)

OffsetContains


-16SizeInt with reference count.
-8SizeInt with actual string size.
0Actual array of char, null-terminated.



Widestring types

On Non-Windows platforms, the widestring type is equal to the unicode string type.

On Windows platforms, a widestring is allocated on the heap, much like a unicode string, and is terminated with a double null. In difference with a unicode string, it is not reference counted.