Strings


The string data type is the most widely used data type in standard applications. When you have to work with string values, you can use one of the available string types or an array of characters. Delphi allows you to use the assignment operator to assign a string value to a character array.

var   s: array[0..255] of Char; begin   s := 'Hello'; end.

Since a character array is statically allocated, the size of the array cannot be changed during run time and always uses 256 bytes of stack memory, regardless of how many characters the array actually holds.

Delphi still supports the old Pascal string type, but in Delphi, this string type is called a ShortString. A ShortString, just like a character array, is statically allocated and always uses 256 bytes of stack memory. A ShortString can hold a maximum of 255 characters. The first character, at index 0, contains the length of the string — the actual number of characters the ShortString holds.

The following example shows the length and memory size of a ShortString variable. To find out how many bytes a variable occupies, you can use the SizeOf function.

Listing 6-11: ShortString facts

image from book
program Project1; {$APPTYPE CONSOLE} uses   SysUtils; var   s: ShortString; begin   s := 'Borland Delphi';   WriteLn('ShortString Length = ', Length(s)); { 14 }   WriteLn('S[0] Length = ', Ord(s[0])); { 14 }   WriteLn('ShortString Size = ', SizeOf(s)); { 256 }   ReadLn; end.
image from book

The ShortString type is superseded by the AnsiString type for many reasons. If we use a lot of short string values, they waste a lot of valuable space. On the other hand, working with a really long string is impossible because the ShortString type limits the length of the string to 255 characters.

To solve the problem of wasting space, Delphi enables us to explicitly define the maximum length of a string. The syntax for a string declaration with an explicitly defined length is:

VariableName: string[Length];  UserName: string[15];

To declare a string with explicit length, we used the reserved word string that is usually used to declare an AnsiString (long string) variable. When we explicitly define the string length, the resulting variable is a ShortString that occupies Length + 1 bytes. This additional byte is the first byte that contains the length of the string. In this case, UserName occupies 16 instead of 256 bytes of memory.

By default, the reserved word string represents the AnsiString data type that can hold up to 2 GB of characters. The meaning of the reserved word string can be changed with the $H or $LONGSTRINGS compiler directives. Because string is a much better data type than the ShortString, there is no need to change this behavior.

The main difference between ShortString and string data types is that strings are dynamically allocated on the heap. This means that their size can change during run time and that they don't occupy more space than they need to. Even though strings are dynamically allocated, we don't have to worry about the allocation/deallocation part of working with strings because Delphi does that automatically.

Another major advantage of the string data type is that it is reference counted. Each time you assign a ShortString to another ShortString, all 256 bytes are copied. Unlike the ShortString type, when you assign a string to another string, the string isn't copied. After you assign a string to another string, the destination string only points to the first character of the source string and the reference count of the source string increases by one. So, you only have a single copy of the string value in memory, but several variables use it. When you assign one string to another, no matter how large the string is, only 4 bytes of memory are copied. The following example shows two variables that use the same value.

var   orig: string;   clone: string; begin   orig := 'Borland';   clone := orig; end. 

Delphi only makes an actual copy of the string when you change the contents of the string. When you change the contents of the string, the string becomes "standalone" and the reference count of the original string is decremented. When the reference count of a string reaches 0, Delphi automatically deallocates the memory occupied by the string.

var   orig: string;   clone: string; begin   orig := 'Borland';   clone := orig;   orig[Length(orig)] := 'D';   WriteLn(orig); { BorlanD }   WriteLn(clone); { Borland }   ReadLn; end.



Inside Delphi 2006
Inside Delphi 2006 (Wordware Delphi Developers Library)
ISBN: 1598220039
EAN: 2147483647
Year: 2004
Pages: 212
Authors: Ivan Hladni

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net