Some string APIs don't store the size, some include the bytes in the
"struct" itself using the struct hack, in C. For instance ustr uses
3 bytes more than strdup(), by default, for "small" strings[1] -- which
includes a size, length, reference counting and a "memory failure occured"
error.
On the other extreme, C++ strings or Vstr take up much more than
12/24[2] bytes (because they are designed to be used differently) and
strbuf itself takes up 16/20[2] bytes.
Note that I'm not saying you shouldn't use a decent string API, quite
the opposite, but assuming that "whatever is embedded in the language"
is good enough for all purposes is far from reality.
[1] 1-255 bytes is "small", for obvious reasons you need one more byte
after that (note that "", 0 byte strings, don't take any bytes to store
with ustr).
[2] 32bit/64bit sizes
--
James Antill -- james@and.org
C String APIs use too much memory? ustr: length, ref count, size and
read-only/fixed. Ave. 44% overhead over strdup(), for 0-20B strings
http://www.and.org/ustr/
-