next up previous contents
Next: Moving size_and_flags inside data Up: Design Trade-offs Previous: Embedding Flags in Length   Contents

Embedding buff_size in data

The buff_size parameter is embedded in the string data according to the STR_EXTRA_BUFFER bit. Another approach would have been to reserve 4 additional bytes of overhead for buff_size. I decided to take the approach of saving per-string overhead.

One disadvantage of embedding buff_size in the data is that, in the case where buff_size - length < 5, there are not enough bytes to store both the NULL and buff_size. In this case, STR_EXTRA_BUFFER is cleared and 1 to 3 usable *data bytes are lost in a memory hole. Understand that this is not the same as a memory leak. A memory leak is a situation where a program loses track of memory, leaving the memory inaccessible until the program terminates. In this situation, a free(data) will correctly return the bytes in the memory hole to the program. This will happen when the string is destructed, when it is resized larger, when it is compacted, or when it is manually reallocated by the user.