4 ms·
Everything. The information density of the statement is enormous. Do you know the precendence of --, > , ++ and = by heart? Also if you lose a space you are
by bsder 13y ago
Everything. The information density of the statement is enormous. Do you know the precendence of --, > , ++ and = by heart? Also if you lose a space you are relying on compiler maximal munch to process (count-->0) as --, > and not -, ->. Did p get incremented or did p get incremented? etc.
Finally, the same people who write that kind of code also write things like f(x++, (x+1)) without realizing that this is undefined in C.
Also, as an added bonus, postincrement/decrement sometimes creates extra unwanted copies in C++ so you need to use the pre- versions.
Compilers are good enough to handle tight loops like this even on very low optimization settings. Better to write that stuff out.
I have found that as I program more, I kind of agree with Crockford that ++, and -- are evil and should always be replaced by pp += 1; What that does is force you to avoid actions in conditional expressions and pull the mutators inside the block. It also forces you to be explicit about when you expect the increments to happen.
- userbinator 13y ago> Do you know the precendence of --, > , ++ and = by heart? > Also if you lose a space you are relying on compiler maximal munch to process (count-->0) as --, > and not -, ->. Those are fundamental things that should be second-nature to you if you claim to really know C, just as a mathematician would know that exponentiation has higher precedence than multiplication which has higher precedence than addition. > Did p get incremented or did p get incremented? There's an easy and concise mnemonic for the precedence of ++/-- and dereference - just remember that this is a strcpy, as shown in K&R: while(*dst++ = *src++) ; So here it is the pointer that is incremented, and not what it points to. > Finally, the same people who write that kind of code also write things like f(x++, (x+1)) without realizing that this is undefined in C. Those are the people who think they know C, but actually don't; it's an issue of not knowing what you don't know, and in such situations you should either seek to find the answer, or be conservative.
- chetanahuja 13y ago"Those are fundamental things that should be second-nature to you if you claim to really know C" That's a bogus argument. There's an easy way to write code that's readable and maintainable by mixed teams and then there's this macho way of showing off to no particular advantage. - just remember that this is a strcpy, as shown in K&R: while(dst++ = src++) ; Or -- just don't. Just write code that doesn't rely on every reader to mentally clear through a dense thicket of precedence and side-effects to understand your strcpy and get your job done in a pragmatic fashion with 4 lines of easily understood C code instead of 1 line that makes your heart glow with momentary pride.
- JoeAltmaier 13y agoNot that dense. Difference is professional vs hobbyist. Don't imagine the professionals are going to write down to the hobbyist.
- chetanahuja 13y agoSorry our definitions of what constitutes a "professional" are entirely different. The best teams I've worked in take great care to avoid needless complexity in code. Readability and maintainability take precedence over everything else... (even performance unless proven otherwise by measurement). Given the kinds of projects I've been part of are already swarming with (externally imposed) system complexity to begin with, an attitude of "I write dense code since I'm a professional" won't get anyone very far in such teams.
- JoeAltmaier 13y agoI'm sorry if that seems dense to you. That's pretty much beginner C stuff; after years of writing it, its absolutely transparent to an experienced programmer. I'm astonished at the vitriol; every language takes some learning whether it be lambdas or annotations or declarations or whatever; if it seems opaque to you, perhaps some reading is in order.
- chetanahuja 13y ago"That's pretty much beginner C stuff" And yet we started off discussing a serious bug in libc discovered in exactly that kind of code. That's the libc in Android!! My argument is that the extra cognitive overhead of digesting needless complexity leads to the original authors and reviewers of the code to miss other obvious bugs. I'm astonished at the vitriol I'm sorry if my straightforward and respectful response to your dismissive and patronizing remarks sounds vitriolic to you.
- JoeAltmaier 13y agoApology accepted. I didn't mean you; there is plenty of vitriol elsewhere in this thread.
- bsder 13y agoFor any junior engineers keeping score at home: NEVER WRITE THIS CODE! EVER! This is the code that gives us the classic buffer overflow vulnerability all because checking for a length isn't elegant. Also, how does this code work if what one of those pointers points to are declared volatile? What if both pointers point to volatile things? What happens if one of those pointers crosses through 0 because it rolled over from max? Thanks. I doubt you could have provided a better example of the precise problem.
- Dylan16807 13y ago> This is the code that gives us the classic buffer overflow vulnerability all because checking for a length isn't elegant. The few characters it would take to check length didn't effect this API, it was just part of the decision to use null terminated strings instead of storing lengths. >Also, how does this code work if what one of those pointers points to are declared volatile? What if both pointers point to volatile things? I can't imagine any way in which volatile would cause problems. What are you pondering? strcpy doesn't take volatile pointers anyway. > What happens if one of those pointers crosses through 0 because it rolled over from max? That's just a very specific type of buffer overflow. strcpy is the wrong function to use in such a scenario. > Thanks. I doubt you could have provided a better example of the precise problem. strcpy is for very specific situations. Writing it in a simple way isn't the problem. It's not like it's even theoretically possible for strpy to avoid overflowing. It doesn't have enough information, only two pointers. In short, this is a bad API, it is not a bad implementation.
- bsder 13y ago> I can't imagine any way in which volatile would cause problems. What are you pondering? Termination condition. Which data applies and does it have to be reread after the assignment?
- userbinator 13y agoIt is read once and written once, and the value that was read is the value of the expression. Note that this "elaborated" version, while(*src) { char c = *src; src++; *dst = c; dst++; } if the pointers were volatile, would actually read the source value twice.