95 of 172
ZBasic Language Reference 88 ZX Microcontroller Family 3.26.3 String Address and String Type Generally, you needn’t be concerned about the technical details regarding strings described in the preceding section. For certain special situations, however, it may be useful directly access a string variable’s inner data components. The StrAddress() function works with all of the string types described above and will return the address of the first character of the string storage. Note, however, that for dynamically allocated strings if the string’s length is zero the returned address will be of no use (it will generally be zero). Moreover, the returned address may be an address in RAM, in Program Memory or in Persistent Memory. Depending on where the string’s characters are stored you must use different System Library Functions to retrieve the string’s characters. You can use the System Library function StrType() to determine the nature of the string’s storage. It will return the second byte of the string variable’s storage space. See the tables above for the values and their meaning. BasicX Compatibility Note In BasicX compatibility mode, neither StrAddress() nor StrType() is available. 3.27 Controlling the Heap Size and Main() Task Stack Size The diagram below illustrates how RAM is allocated between defined variables, task stacks and the heap. The static allocation area, located at the beginning of RAM, includes all module-level variables defined by your application and all statically allocated system variables. This includes the task stacks that are statically allocated by your application. For native mode devices, the task stack for Main() occupies all of the RAM between the end of the static allocation area and the heap limit. The task stack grows downward from the heap limit while the heap grows downward from the end of RAM. By default, the heap limit is set at 512 bytes from the end of RAM. For VM mode devices, the space between the end of the static allocation area and the end of RAM is shared by default between the Main() task stack and the heap. The task stack grows upward from the end of the static allocation area while the heap grows downward from the end of RAM. There is no specific heap limit in the default case but you may specify one as described below. If a heap limit exists, the heap will not grow beyond that point. When the heap is exhausted, further allocation attempts will fail, possibly resulting in malfunctioning of your application. The heap is used for storing most RAM-based strings (but not BoundedString or fixed-length string types) including strings that are returned by functions you define in your application and System Library functions that return strings. Additionally, for devices which use internal Flash memory for Program Memory, when a Program Memory write operation is performed a temporary buffer is allocated from the heap. The size of this buffer is equal to the Flash page size for the microcontroller, typically 256 bytes. This additional temporary use of heap space must be considered if your application performs write operations on Program Memory. Lastly, the System Library function System.Alloc() may be used by your application to allocate memory from the heap. All of these uses must be considered when determining how much heap space is needed by your application. The function System.HeapHeadRoom() may be helpful for determining the minimum heap size for your application. You can specify the heap limit setting in several different ways, each of which may be useful in different circumstances. Perhaps the simplest method of setting the heap limit is to specify the desired size for the Main() task stack. This can be done using the directive Option MainTaskStackSize in your Main module or by using the compiler option –-main-task-stack-size. In both cases, the heap limit is set by adding the specified size to the address of the end of the static allocation area.