A
String is an array of characters.
A
String declared without the
size parameter is dynamically resized depending on the length of the string. The length can range from 0 bytes to 2 gigabytes. A descriptor contains a pointer to the actual string, the length of the string, and the amount of space allocated for it.
VarPtr will return a pointer to the descriptor, while
StrPtr will point to the actual string.
Because of the hidden descriptor with a
String, manual allocation of space, for example using the memory allocation function
CAllocate (preferentially), for a
String is not encouraged. The common way to ensure a certain amount of space is reserved for a
String, to prevent unnecessary allocations inside a loop for instance, is to use the
Space or
String functions.
Nevertheless if necessary, dynamic allocation may be carefully used by means of the memory allocation functions
Allocate,
CAllocate,
Reallocate (see precautions for use) and string pointer (which is a pointer to a string descriptor, not string data). When memory is allocated to hold string descriptors, the string must always be destroyed (setting to
"") before deallocate each string descriptor (allowing to deallocate the memory taken up by the string data), otherwise, it is not possible to deallocate it later, and it may induce memory leak in the program continuation.
Despite the use of the descriptor, an implicit
NULL character (
Chr(0)) is added to the end of the string, to allow passing them to functions in external libraries without making slow copies. FreeBASIC's internal functions will ignore this character, and not treat it as part of the string.
A
String declared with a
fixed size is a QB-style fixed length string, with the exception that unused characters are set to 0, regardless of what "-lang" compiler option is used. It has no descriptor and it is not resized to fit its contents. As in QB, if data overflows the size of the string, it is truncated on the right side.
Fixed length strings are also terminated with a
NULL character, and so they use
size + 1 bytes of space. This
NULL terminator may be removed in future, to prevent the redundant character complicating data layout in user-defined
Types.
String variable names need not end in a dollar sign
$ as in other dialects of BASIC. In
lang fb variable suffixes, including the dollar sign, are disallowed entirely.