91 of 158
85 3.26.1 User-defined Type Details The user-defined types in ZBasic are enumerations and structures. Enumerations are implemented using a two-byte value to represent the enumeration member value. Structures are laid out in memory exactly corresponding to the order in which the members are defined in the structure definition. Bit and Nibble type members are aligned on bit and nibble boundaries, respectively. All other member types are aligned on a byte boundary. Due to these alignment rules and depending on the specific structure definition, there may be unused bits and/or nibbles within the structure. The number of bytes consumed by a structure variable is the sum of the sizes of the members including the unused bits. 3.26.2 String Data Type Details For the string data types, the storage requirements and implementation details vary depending on compiler command line options, Option Directives and the type of string. For the String data type, if Option AllocStr is enabled (as it is by default for ZBasic modules) each string variable requires four bytes of storage in User RAM space plus additional space allocated from the dynamic memory allocation heap to hold the characters of the string. This string storage strategy is called dynamic string allocation because the space to hold the string’s characters is dynamically allocated and will grow and shrink as need be to accommodate the string value assigned to it. One additional advantage to using dynamically allocated strings is that the string storage will never be overrun because it is changed dynamically and limited to the maximum size of 255 characters automatically. The 4 bytes of User RAM for a dynamically allocated string variable are used in the following manner: Dynamically Allocated String Storage Layout Offset Length Description 0 1 The current string length, in bytes. 1 1 A marker identifying the string location: &He0 – RAM (allocated from the heap) &He2 – Program Memory &He3 – Persistent Memory &He4 – RAM (not allocated) &He5 – RAM (not allocated, limited to 2 chars max.) &He6 – RAM (pointer refers to a statically allocated string) 2 2 For type &He5, the one or two characters of the string. For type &He6, the address of a statically allocated string variable (see below). For all other types, the address of the first character of the string. For statically allocated strings, including Bounded Strings and Fixed Length strings, the space allocated for the variable is used in the following manner: Statically Allocated String Storage Layout Offset Length Description 0 1 The current string length, in bytes. 1 1 A marker identifying the string characteristics: &H00 – fixed allocation, variable length &Hff – fixed allocation, fixed length 2 N The characters of the string where N is the defined string length. For Fixed Length strings the byte at offset zero will be constant and the byte at offset 1 will have the value &Hff to indicate that it is a Fixed Length string.