3 ms·
>A fixed size length field commits you to a maximum string size size_t would be enough, but I guess it would be too much of an overhead.
by leni536 11y ago
>A fixed size length field commits you to a maximum string size
size_t would be enough, but I guess it would be too much of an overhead.
- kzrdude 11y agoDebating the size of the size field is a folly: It's a struct with one char pointer, and a length field. For alignment / padding reasons, the length field with use the size of a pointer anyway, so `size_t` is entirely appropriate (and never too small).
- gpderetta 11y agoA relatively common implementation of strings (as compared to slices) is to store the length before the char array. The length field can be encoded with a variable size encoding. The string itself is just a single pointer. If you have a real garbage collector of course using the same representation for strings and slices (two pointers or pointer+len) is appropriate.