3 ms·
> The trick is the size is hidden before the adress of the buffer.("Learn this one simple trick that will change your life for ever"). The length-prefix string
by WalterBright 4y ago
> The trick is the size is hidden before the adress of the buffer.("Learn this one simple trick that will change your life for ever").
The length-prefix string has a major problem - it cannot be sliced to produce another length-prefix string. It has to be copied. Instead, using a phat pointer (size_t length, char* ptr) works very, very well. We've been using it in D for 20 years.
I've proposed it for C, too:
https://www.digitalmars.com/articles/C-biggest-mistake.html https://www.digitalmars.com/articles/C-biggest-mistake.html
- torstenvl 4y ago> it cannot be sliced to produce another length-prefix string Come again? Of course it can. It can't be done in place, mind you, but that's a pretty bad way to do any string slicing, regardless of implementation, in a manual memory management environment. Do most programmers expect their slices to result in undefined behavior if they release the larger string they were made from? I doubt it.
- alcover 4y ago> Come again? Of course it can. Oh come on.. I'm pretty sure Walter meant taking a view kind of slice. Obviously one can always copy part of a string, but that's not what slice implies I think. > It can't be done in place, mind you, but that's a pretty bad way to do any string slicing, regardless of implementation, in a manual memory management environment. It's not bad. It's the best, most efficient way. O(1)-ish. > Do most programmers expect their slices to result in undefined behavior if they release the larger string they were made from? I doubt it. That's what copy-on-write is for : release of the parent is blocked until no views are left on it. I made a C String lib using CoW. It works well: https://github.com/alcover/buffet https://github.com/alcover/buffet
- WalterBright 4y agoThe length-ptr strings are very fast and efficient: 1. don't have to make a copy 2. don't have to scan to find the length 3. don't have to load the string into the cache to find its length As a bonus, they're memory safe.
- quelsolaar 4y agoThe problem with arrays is not that they decay to pointers, its that they arent pointers to begin with. This: int x[10]; Sould mean "put 10 ints in memory, and make x the pointer to it.". The thing that messes this up is sizeof. sizeof(x) doesnt give the size of the pointer like it should, it gives you the size of the array. If that was fixed (obviusly it can without breaking everything) then things would be much better and consistent.
- WastingMyTime89 4y agoYes, sizeof has a primitive as a weird behaviour when used in the scope of a continuous allocation. I agree it’s unfortunate. But "arrays" definitely are pointers. I put "arrays" in quote because C has nothing I would personally call an array. It’s just contiguous memory allocation. It’s to the point that 10[a] and a[10] are desugared to the same thing.