News for hackers. Stuff that matters.
Re:Show HN by WalterBright on Saturday December 03, 2022 @01:44PM (#33846079)
> 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

Re:Show HN by quelsolaar on Saturday December 03, 2022 @02:23PM (#33846410)
The 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.

Re:Show HN by WastingMyTime89 on Saturday December 03, 2022 @03:39PM (#33847087)
Yes, 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.

Re:Show HN by torstenvl on Saturday December 03, 2022 @02:13PM (#33846321)
> 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.

Re:Show HN by alcover on Saturday December 03, 2022 @02:38PM (#33846544)
> 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

Re:Show HN by WalterBright on Saturday December 03, 2022 @06:41PM (#33848742)
The 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.