News for hackers. Stuff that matters.
Re:Why is strlen so complex in C? by raverbashing on Monday August 26, 2019 @05:15PM (#20803847)
It's such a shame that C gets some things like strings so wrong.

Writing in C shouldn't have to be so painful, but I guess they were the pioneers in a lot of things and the "high level assembly" idea stuck. (Premature optimization?)

Also having objects and method calls, even if it's syntactic sugar deep down, is the best kind of syntactic sugar

Re:Why is strlen so complex in C? by compiler-guy on Monday August 26, 2019 @05:33PM (#20804024)
There was nothing "premature" about C's "high-level assembly" idea. It is somewhat archaic now, but C took off because it was so, so much better than normal assembly.

If handling strings in plain C is hard, handling them in assembly is 10x as hard.

Re:Why is strlen so complex in C? by saagarjha on Monday August 26, 2019 @05:43PM (#20804089)
repne scasb disagrees with you :)
Re:Why is strlen so complex in C? by compiler-guy on Monday August 26, 2019 @06:17PM (#20804327)
As would many other assembly instructions on many other architectures.... and thereby proving my point. :-)
Re:Why is strlen so complex in C? by wahern on Monday August 26, 2019 @08:19PM (#20805044)
C's model is great for implementing complex, efficient data structures. Pointers are an amazing abstraction. But in many respects strings are the antithesis of a data structure, and unsurprisingly C falters hard. The problem is that most programmers don't build data structures; they build Rube Goldberg machines for munging strings, even when they're using low-level languages.

Data structures to most programmers are things you import into your Rube Goldberg machine. They don't understand that good programs are made of layers of meticulously crafted, bespoke data structures where the final product is effectively one giant data structure tailored for a particular task, with interfaces to match.

It follows that good C programs don't do very much strings munging even if all they do is process text. The text is systematically atomized from the very beginning. The same is true for good programs in any other language.

Re:Why is strlen so complex in C? by klingonopera on Monday August 26, 2019 @11:39PM (#20806140)
"It's such a shame that human beings get some concepts like strings so wrong." - C language

Strings are "unnatural" to the binary system, it's a human concept.

Re:Why is strlen so complex in C? by saagarjha on Monday August 26, 2019 @05:26PM (#20803952)
I think you're looking for C++, or maybe Rust.
Re:Why is strlen so complex in C? by MadWombat on Monday August 26, 2019 @06:51PM (#20804530)
This is kind of the point of C. It maps directly to the hardware. There are very few decisions the compiler has to make about the code. And it is for that reason that C does not allow for higher level abstractions. On one hand, it is a pain to write. On the other hand, you know exactly what is going on under the hood and nothing is ever going to stop you from shooting yourself in a body part of your choice :)
Re:Why is strlen so complex in C? by ncmncm on Monday August 26, 2019 @08:08PM (#20804977)
C gives the illusion of mapping to the hardware. Hardware manufacturers have moved heaven and earth to help maintain the illusion. It finally got too hard and they had to admit atomics and cores ro the model. The next watershed will be along soon enough, but the ability to present as a C machine has long been the gateway to success or failure of a new hardware architecture. Myriads of arguably superior machine designs have foundered on "but can I code for it in C?"
Re:Why is strlen so complex in C? by kllrnohj on Monday August 26, 2019 @07:19PM (#20804696)
> This is kind of the point of C. It maps directly to the hardware.

No it doesn't. It didn't in the past, it was a high-level abstraction. And it doesn't today, because CPUs don't behave at all like C's defined virtual machine.

Re:Why is strlen so complex in C? by justinmeiners on Monday August 26, 2019 @07:24PM (#20804731)
> CPUs don't behave at all like C's defined virtual machine

What "virtual machine" are you referring to?

Re:Why is strlen so complex in C? by anticensor on Sunday September 08, 2019 @03:47PM (#20912936)
One that ISO 9899 refers to as its abstract machine.
Re:Why is strlen so complex in C? by saagarjha on Monday August 26, 2019 @07:32PM (#20804787)
The one defined in the standard.
Re:Why is strlen so complex in C? by tinus_hn on Monday August 26, 2019 @10:05PM (#20805709)
How can it map directly to the hardware and be portable at the same time?