After reading this chapter, you will be familiar with what a
like in memory, what the time/space trade-offs are between
char*, and how
Str supports features such as automatic memory
management. If you are more interested in using
Str than understanding
how it works, (or if you find yourself getting bored) you can safely skip to
The first design challenge when creating
Str was to create a memory
structure that was space efficient but still met the goals of automatic
memory management and efficient operations. Figure 3.1 shows the final
Looking at figure 3.1:
datafield is a pointer to a buffer that holds a
char*style array. In the empty string case, the length of the string is set to zero and the data field is set to NULL (0).
datais pointing to string buffer data on the heap. In the
size_and_flagsfield, we have the value
0x80000005. This means that the string is 5 bytes long and has the
STR_ALLOCATEDbit set. When
free(data)when it is destructed.
size_and_flagsalso has the
STR_EXTRA_BUFFERbit set. When this bit is set, a 4-byte field named
buff_sizewill immediately follow the terminating NULL in the string's data.
size_and_flags. This allows many string operations to operate efficiently as they do not have to walk the string to find its length.