Live data from Hacker News

Linux eliminates the strncpy API after six years of work, 360 patches

phoronix.com

191–200 of 340 posts

Re: Linux eliminates the strncpy API after six years of work, 360 patches

#191
post #45

Earlier quoted context omitted.

The size overhead of that is 2*sizeof(int) while the overhead of null termination is sizeof(char). If I remember the standard right, the former is worse by at least sizeof(char), and usually more in practice. This used to matter, sometimes still does.

Well, not saying to always use it, but if the string size is big enough, the overhead of 2 ints becomes relatively vanishing. For generic dynamically sized strings it probably has more advantages than disadvantages. But in any case, sure, if every single byte matters or some structure requires specific memory layout, then fine. I just don't think these things are the majority of use cases. Keep in mind that the cache…

I am a terrible hobby c programmer that doesn't understand pointers but surely a symmetric approach doesn't have the overhead or the bug. that is to say that if the language was designed to work in single bit pairs of a string character in conjunction of a string length character assuming a fail safe design of one dummy string character then if a bug happens in the code then there's no overflow because the length can never be shorter than the character.

Re: Linux eliminates the strncpy API after six years of work, 360 patches

#192

I have in the past made fun of the Linux kernel devs, supposedly some of the best C developers in the world, for not knowing how to make stringbuffer and stringview types, but to be fair to them we didn't have the consensus we have today on the topic. You know who did have the right idea though? Dennis Ritchie, who proposed a fat pointer type for C all the way back in 1990. Would have made for a perfect addition to C…

> but to be fair to them we didn't have the consensus we have today on the topic.

This is my pet peeve of teamwork. We can choose solutions A, B or C. Each has upsides and downsides. We debate for two weeks, then we choose nothing.

Re: Linux eliminates the strncpy API after six years of work, 360 patches

#193
post #138

Earlier quoted context omitted.

Why would a pascal string be any shorter than a C string? A C string is one pointer reaching all of memory, a Pascal string is two pointers reaching all of memory

A pascal string is a single byte with the length, followed by the data. Some implementations use more bytes for the length data, such as Delphi which changed over to a 4 byte prefix length, though those aren't technically Pascal strings anymore. I can't find anything about a Pascal string being two pointers?

It is conceivable, for both Pascal and C, to have more than one string implementation side by side, so the developer can choose to use the best-fitting one.

In C++23, variant permits to do what Rust's typed enums introduced (e.g. Result sum type that is either a "real" result - with result type - or an error - with error type -, each strongly typed).

If you do that, a definition like

  class IString { /* basic string functions */ };

  class MiniString : public IString {};
  class CZeroTerminatedString : public IString {};
  class PascalString : public IString {};
  class CppString : public IString {};

  use String = std::variant; // define one type for all impl.
permits to define string functions that operate over the sum type String, and which use the methods defined in the interface IString, and which then work for all string implementations.

The developer can then pick the most suitable implementation, i.e. CMiniString for very, very short strings (that fit into 64 bits, so approx. Sum types are a type-safe and memory-preserving way to do what in the older days was sometimes implemented using a "union {}" (which was not type-safe).

Re: Linux eliminates the strncpy API after six years of work, 360 patches

#194

I have in the past made fun of the Linux kernel devs, supposedly some of the best C developers in the world, for not knowing how to make stringbuffer and stringview types, but to be fair to them we didn't have the consensus we have today on the topic. You know who did have the right idea though? Dennis Ritchie, who proposed a fat pointer type for C all the way back in 1990. Would have made for a perfect addition to C…

> But we did get _Generic and VLAs! Party hard.

VLA has been demoted to an optional feature in C11 (good).

IMHO the current main problem is that the C stdlib is stuck in the K&R era and the stdlib APIs haven't even been updated to the language features added in C99 (e.g. make use of struct args and return values). A range struct (ptr/size pair) in the stdlib and new or updated string functions to use such ranges would already go a long way.

Re: Linux eliminates the strncpy API after six years of work, 360 patches

#195

Earlier quoted context omitted.

Linux doesn't exist in the 1980s, Linus started this work in the 1990s. But yes, the string slice type should have existed in C89 and it's very obvious from here that not having something of this sort - maybe what Rust would call &[u8] the reference to a slice of bytes - was a big problem for C. The correct way to represent this is what's called a "fat pointer". A pair of values, one is a conventional "thin" pointer…

That's what pascal did back in the day, but 255 byte strings were all that was needed back then so only a byte was needed to store the length. Does that still sound maintainable? Anyhow, some developers put data into strings when they shouldn't, and require doing that in the APIs they publish. Strings, whether NUL terminated or with stored length, aren't always the best choice architecturally so making them easy to u…

Are you muddling the Pascal strings with the string slice?

Re: Linux eliminates the strncpy API after six years of work, 360 patches

#196

Earlier quoted context omitted.

> I remember why C stayed what it is at least: elitism and gatekeeping. If that was the goal, it failed horribly - the gatekeeping didn't work because the popularity exploded. > You're telling me OCaml / Rust / Haskell compile to fairy pixie dust? Obviously their compilers figured it out and it works. I said nothing of the sort.

You asked how sum types work in assembly. I'm telling you that at least 3 compilers figured that part out.

> You asked how sum types work in assembly.

No, I didn't - I asked how sum types were supposed to work in an era of 64KB memory systems.

Re: Linux eliminates the strncpy API after six years of work, 360 patches

#197

Earlier quoted context omitted.

Sum types, of course.

How do you expect to use sum types in assembly? Remember where C came from and why it was designed the way it was.

A naive sum type is just a tag plus a payload. There is no problem here. If you have enums you could have had sum types.

The historical argument and appeal to assembly is illogical here. The only real argument is that niche value optimization is too complex or too clever for the time so even if sum types were in C, nullable pointers would still exist either way.

Re: Linux eliminates the strncpy API after six years of work, 360 patches

#198

Earlier quoted context omitted.

You asked how sum types work in assembly. I'm telling you that at least 3 compilers figured that part out.

> You asked how sum types work in assembly. No, I didn't - I asked how sum types were supposed to work in an era of 64KB memory systems.

They don't need extra memory in Rust for the case of nullable pointers.

The boring cases require an enum tag in C too.

By bringing up the one thing that doesn't matter, your argument becomes purely ideological.

Re: Linux eliminates the strncpy API after six years of work, 360 patches

#199
post #160

Things that have bugged me for 40 years... * NUL terminated strings (and now, non UTF-8 encoded strings on input/output) * Using LF or CR or CRLF as line terminators, and pipe/comma-delimited fields when there were other unambiguous ASCII characters that could have been used (eg, GS, FS, RS) that would have made the encoding/decoding of line termination an I/O thing keeping HT/VT/CR/LF/FF as literally print related c…

> non UTF-8 encoded strings on input/output

UTF-8 on stdin/stdout works perfectly fine (unless you are on Windows of course, which is stuck in in the early 90s when it comes to international text encoding).

> Using LF or CR or CRLF as line terminators

This is also an operating system convention, and it would be better if programming languages wouldn't try to "guess" the correct line endings, since this causes more problems than it solves - but again, this is mostly a Windows specific problem, and it's Microsoft's job to finally bring Windows into the current century.

Re: Linux eliminates the strncpy API after six years of work, 360 patches

#200
post #186
post #164

Earlier quoted context omitted.

C was specifically developed to allow Unix to be ported. It was a systems programming language and the first well known/successful one. There was BCPL and then B before that, which is why the language is called "C". Pascal was considered a teaching language, along with "Algorithms + Data Structures = Programs" by Wirth etc. The UCSD P-system was one of the first "IDEs" and used Pascal and a bytecode interpreter of th…

Successful systems languages trace back all the way to JOVIAL in 1958. You missed quite a few between JOVIAL, and C being adopted outside Bell Labs. Modula-2 was as widely available as C was outside UNIX and universities with access to UNIX source code. It took a while for proper C to actually be "used for everything else", until the early 1990s actually, and by then anyone sensible would be much better with Typescri…

> Typescript for C

You mean "C with Classes", later to be replaced by "C++" (Stroustrup pick this as favorite from a list of candidate names he crowdsourced) as implemented by Cfront.

Post reply on HN