4 ms·
I wish this article talked about why people choose to do and not do these things. So much of this stuff is the kind of thing people new to C should not be doin
by voidlogic 13y ago
I wish this article talked about why people choose to do and not do these things.
So much of this stuff is the kind of thing people new to C should not be doing. Minimizing use of preprocessor directives leads to more maintainable C. Use them where they are needed and not anywhere else.
Also, for the swapping they show:
a = a ^ b;
b = a ^ b;
a = a ^ b;
They fail to point out it's (platform dependent) performance implications. The above code is faster then the below code on PowerPC/POWER for example, but slower on x86/amd64.
int tmp = a; //or: register int tmp = a;
a = b;
b = tmp;
- dicroce 13y agoToo many older developers are carrying around biases today that used to be true (like the performance issue you pointed out).
- vonmoltke 13y agoI'm still trying to shake off their influence. :P
- yoklov 13y agoI'd be very annoyed if my compiler didn't turn the latter into the former on platforms where it was more efficient.
- vowelless 13y agoRelevant: http://en.wikipedia.org/wiki/XOR_swap_algorithm#Reasons_for_avoidance_in_practice http://en.wikipedia.org/wiki/XOR_swap_algorithm#Reasons_for_...
- vsbuffalo 13y agoNeat point, thanks for pointing it out. I love C (even more so after recently learning and using C++), but one minor thing is that frustrating is how hard it is to both (1) get really good at C and (2) the lack of great books on high-level C. In other languages I use, I can get better by studying the source of programmers better than me. With C this is risky, as many large projects have messy code bases, and through their complexity hide the fact that one of C's great features is that it can be extremely lightweight in some projects. The tricks skilled C programmers use are a mix on unadvised hacks (like xor swap) and truly clever, portable, advised concepts. There's also no great C book on higher-level C. Deep C Secrets is good, but perhaps a bit old. If you're using C for data work, you're out of luck (with maybe the exception of Ben Klemen's book, Modelling with Data, which is good but almost uses C too much when a bit of R and python would help). I'd love a book of machine learning and/or computational algorithms in C — with direct data examples.
- intull 13y agoMy apologies for not including this! I'll add it in an edit soon! Thank you!
- CJefferson 13y agoActually, in real use, the XOR swapping is almost always slower, because in practice the temp turns into: Read a into register1 Read b into register2 Write register1 to a Write register2 to b Whereas with the xor, you read the values, then do some pointless xoring, then write them back out. Further, if you are using a and b for anything else, the 'tmp' turns into a simple register renaming, for 0 cost! While ^ swapping makes sense in assembler, it never makes sense in C.
- dima55 13y agoDoesn't even makes sense in assembly, at least on x86, since you can just use the xchg instruction.
- peterashford 13y agoFair point. Macros in particular are frowned upon. Although.... they can be very handy when used sparingly.
- golergka 13y agoYou don't even have to use C to create a preprocessor hell. I'm currently working on Unity3d/C# project which relies on preprocessor directives to behave differently on different platforms, and I would trade it for a runtime checks anytime.