3 ms·
I've used something similar --- a null-terminated string, with a length prefix at a negative offset. Resizeable ones have an "allocated size" field before that
by userbinator 9y ago
I've used something similar --- a null-terminated string, with a length prefix at a negative offset. Resizeable ones have an "allocated size" field before that one. This makes for strings that are both easy to manipulate and compatible with existing functions that only read a regular C string.
- zzzcpan 9y agoBasically this: https://github.com/antirez/sds https://github.com/antirez/sds
- userbinator 9y agoGood to see I'm not the only one who thought of that idea, although my implementation has a different order of the fields (and IMHO better, since it allows for statically allocated strings with only a length field --- a sort of subclassing.)
- timClicks 9y agoSo in a sense implementing Pascal strings in C?
- flukus 9y agoYes. But also "abusing" C's permissiveness with pointers to get pascal strings that interop with standard functions. It get's passed around as a pointer to a char array but the length function would return the value at that pointer - sizeof(size_T). In most languages this wouldn't be possible.
- nikanj 9y agoSo, BSTRs? They’re common in old-timey COM apis