5 ms·
For a long time, I would at this about once a year and almost understand what it does, but never quite getting it. It felt like looking at a hypercube. Then I
by krylon 4y ago
For a long time, I would at this about once a year and almost understand what it does, but never quite getting it. It felt like looking at a hypercube.
Then I read it was about loop unrolling, and suddenly it clicked.
- taneq 4y agoAnd also the fact that C sometimes cares way less than you might think about what goes where. :)
- krylon 4y agoAfterwards, I started to like Duff's description of his device as being a strong argument in the debate on switch's fallthrough behavior, but that he did not know if it was pro or con.
- null0pointer 4y agoAnother good example of this flexibility in C is the inverse array notation (I think that’s what it’s called, turns out it’s impossible to search because all the results are about reversing arrays). arr[index] is equivalent to index[arr] This works because arr points to the memory location of the zeroth index of the array and index is the offset from that memory location. Since addition is commutative so too is the array notation.
- justinsaccount 4y agoRight, arr[index] is sugar for *(arr+index) which is then also *(index+arr) and index[arr] edit: fixed escaped asterisks :-)
- jbverschoor 4y agoThat sounds soo dirty though
- krylon 4y agoIt is[0]. Duff's device, whether you like it or hate it, was a performance optimization at least, saying "idx[arr]" instead of "arr[idx]" does not help in any way and will make some people angry or sad. Contestants in the International Obfuscated C Code Contest will (and probably already have) find delightful ways to use and abuse this syntactical curiosity. [0] My one-track mind always wants to jump to pirate jokes. EDIT: If you like to explore this area, I highly recommend "Expert C Programming - Deep C Secrets" (get the pun? There's a fish on the cover...) by Peter Van Der Linden. It's pretty old (it talks about DOS memory models, among other things), but it is a fun deep dive into the murky corners of C. I wish there was an updated version and/or books covering other languages in that style.
- taneq 4y ago> Contestants in the International Obfuscated C Code Contest will (and probably already have) find delightful ways to use and abuse this syntactical curiosity. The OCCC is where I learned about this in the first place! Surely it counts as useful if it could be used to confuse The Enemy. ;)