Optimizing strings and array allocations
When you create a string, the code allocates memory for its content, copies the content into that memory, and stores the address of this memory in the string variable. Of course, you all know that by now as this was the topic of the previous chapter.
If you append a character to this string, it must be stored somewhere in that memory. However, there is no place to store the string. The original memory block was just big enough to store the original content. The code must therefore enlarge that memory block, and only then can the appended character be stored in the newly acquired space.
As we’ll see further on in the chapter, FastMM4 tries to make sure that the memory can be expanded in place—that is, without copying the original data into a new, larger block of memory. Still, this is not always possible, and sometimes data must be copied around.
A very similar scenario plays out when you extend a dynamic array. Memory...