6 ms·
> inherently wonderful about `while(*this++ = *that++)` As someone who hasn't used C as a primary language in some project for over a decade, I read this and (
by ckastner 4y ago
> inherently wonderful about `while(*this++ = *that++)`
As someone who hasn't used C as a primary language in some project for over a decade, I read this and (1) realize that LHS and RHS both post-increment, (2) don't remember if there is some UB I might be overlooking, (3) realize that the operator is assignment "=", not comparison "==", and I can't even remember what the loop termination criterion here would be. Until "*that++" is equivalent to false, or something?
It may be beautiful to the experienced programmer, but I personally would consider this just "clever" (which is a criticism, not a compliment). It feels like someone needlessly tried to pack everything into a single line of code.
This is something I'd write out more verbosely, if only to make reading it simpler. The compiler will probably generate the same machine code either way.
I'm fully aware that to the seasoned C developer, my criticism might come across as naive. However, even the fully seasoned C developer can get careless, or become tired, and in C, every one of those little things can come back and bite you in those situations.
Edit: removed the double pointer dereferencing remark, must have been an artifact from HN's special treatment of the asterisk.
Edit-Edit: I was probably wrong. I don't think it's possible to create a more verbose version without it affecting performance.
- flohofwoe 4y agoIt's entirely valid C and (assuming this and that are byte pointers) copies a range of bytes until (and including) a zero byte is reached. With a suffficient warning level (e.g. -Wall on gcc, which should always be enabled anyway, together with -Wextra), compilers will complain about the '=' and ask you to add a pair of braces to make clear that this is actually intended: while( (*this++ = *that++) ); It's also one of those cases where the C code matches the output assembly pretty well: https://www.godbolt.org/z/nz1jbz4Er https://www.godbolt.org/z/nz1jbz4Er As far as "obfuscated C" goes, this is a very tame example though, it's just a straightforward usage of language features, which might look strange only when coming from other languages that don't have pointers or a post-increment operator).
- ckastner 4y agoDoesn't that kind of prove my point? The code as written was described as beautiful, yet would not have passed -Wall. It's those little things that can easily get you in C, and there are so many little things to consider.
- flohofwoe 4y agoThat extra pair of braces doesn't make the code 'ugly' ;) And the code without braces is still entirely valid standard C, the warning is essentially just a lint to protect against typos (similar to JS linters warning about '===' vs '=='). PS: let's see if the alternatives would be any more readable: char c; while (c = *that++) { *this++ = c; } ...this is already buggy because it doesn't copy the final zero byte, so the test must happen inside the loop body and also lets try to get rid of the post-increment: while (true) { char c = *that; *this = c; this += 1; that += 1; if (c == 0) { break; } } ...hmm not really any more readable... Let's try with an index... while (true) { char c = that[i]; this[i] = c; i += 1; if (c == 0) { break; } } ...might be a bit easier to grasp when used to other languages, but readability hasn't improved all that much I'd say... For reference, MUSL also just uses the original approach: https://github.com/esmil/musl/blob/master/src/string/strcpy.c https://github.com/esmil/musl/blob/master/src/string/strcpy....
- ckastner 4y agoI was unclear, sorry: I didn't mean to say that the extra braces make it uglier, I meant to point out that something that was described as beautiful was actually flawed. The flaw was minor in this case because the identifier names and lack of body make the intention clear, but my point is that there are a lot of minor things in C that can come and bite you at any time. Edit: You are right, I don't see a way this could have been implemented more readable without sacrificing some performance. First thing I thought of was: void cp(const char* from, char* to) { while (*from) { *to = *from; to++; from++; } } But that does not reduce to the original case.
- gpderetta 4y ago> the output assembly pretty well Ironically, the compiler os likely to recognize this as a strcpy and replace it with a possibly vectorized implementation.
- flohofwoe 4y agoI actually tried to make that happen, but was unsuccessful on GCC and Clang (I've seen this in the past for mempcy() though).
- sfpotter 4y agoCould we try to keep the topic on the article itself instead of complaints about C? It sucks to come and read the comments about this very nice article and have to scroll and scroll until I finally get to comments written by people who actually have something to say. This is a great blog, and the author puts a ton of effort into their posts. It’s hard for me not to view comments like this as being a bit thoughtless and inconsiderate.
- mjburgess 4y agoThat's why comments collapse. There's little more useful to say here, other than I don't think most people would agree with your view of what HN is. It's a discussion forum, not an exegetical seminar. "On topic" is whatever the topic of discussion is; and this is not constrained to the article.
- dilap 4y agoSince we're already way off-topic, allow me to share my idea for solving this perennial problem: When commenting, there is a little selector: [ ] My comment is on-topic and positive to neutral [ ] My comment is critical [ ] My comment is off-topic You've got to select one. When viewing, the comment thread defaults to just showing on-topic, non-negative comments, but you can see the other stuff, too, if you want. This solves two seemingly contradictory desires: the ability to read comments on things that interest you without having to fend off waves of negativity and wade through pools of offtopic text and the ability to speak freely.
- Nullabillity 4y agoIt sounds like you're looking for an upvote counter, not a comment section.
- dilap 4y agoNo, that's not it at all. The point is to give a small amount of structure to the comments section, so people can see what they want. Think of it as very roughly analoguous to the different sections of a newspaper.
- deleted 4y ago[deleted]