8.2.7 String types

Ansistring types

The ansistring is a dynamically allocated string which has no length limitation. 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).

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


-8Longint with reference count.
-4Longint 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.

Widestring types

A widestring is allocated on the heap, much like an ansistring. Unlike the ansistring, a widestring takes 2 bytes per character, and is terminated with a double null.