News for hackers. Stuff that matters.
Re:Designing a better strcpy by loup-vaillant on Saturday June 19, 2021 @06:10PM (#27564956)
Actually, it's 7 bytes (you can remove `\0` at the end), and that's if you insist on being able to exceed 4GB with your strings.

Personally, depending on the use case, I'd be willing to have strings limited to 256 bytes (no overhead), 64K bytes (1 byte of overhead), and 4G bytes (3 bytes of overhead). Though combining them all might be quite the nightmare…

Re:Designing a better strcpy by codesnik on Saturday June 19, 2021 @06:23PM (#27565044)
you can also have something akin to MIDI encoding with higher bit reserved to mark that next byte is a part of length field, too. up to 128 bytes with no overhead, up to 16kb with one byte overhead, and so on. Such a detail would be easy to hide in the library. But I wouldn't be surprised that alignment and other troubles would eat any possible gain.
Re:Designing a better strcpy by habibur on Sunday June 20, 2021 @03:14AM (#27567870)
> But I wouldn't be surprised that alignment ... troubles would eat any possible gain.

It's string. Alignment shouldn't be a problem. Rather alignment issues are for the pointers to that string.

Re:Designing a better strcpy by WalterBright on Saturday June 19, 2021 @06:45PM (#27565203)
Won't work with UTF-8.
Re:Designing a better strcpy by thrwaeasddsaf on Saturday June 19, 2021 @07:49PM (#27565650)
Works with any encoding and arbitrary binary data. The last byte of the length field doesn't have the high bit set, and whatever follows after that is completely irrelevant.

But still needless complexity for this.

Re:Designing a better strcpy by moonchild on Saturday June 19, 2021 @10:31PM (#27566587)
Sure it is, the length is out of band. In fact, you could even use utf-8 to encode the length!
Re:Designing a better strcpy by dnr on Saturday June 19, 2021 @09:35PM (#27566242)
Don't forget supporting small strings (<= 7 bytes) in the pointer itself using tag bits. No allocation or dereferencing required.
Re:Designing a better strcpy by habibur on Sunday June 20, 2021 @03:06AM (#27567838)
Microsoft's MFC used to do this in their first version. But later ditched it. Maybe they found it not worth the trouble of checking for this condition everywhere. Increase in code.
Re:Designing a better strcpy by gpderetta on Sunday June 20, 2021 @05:19AM (#27568429)
Thse days, in C++, this is routinely done in various std::string implementations (which are both null terminated and have a separate length). In fact the standard was updated to explicitly allow this.