5 ms·
GCC hacks in the Linux kernel
- cabalamat 18y agoCan someone explain this code please: #define min(x, y) ({ \ typeof(x) _min1 = (x); \ typeof(y) _min2 = (y); \ (void) (&_min1 == &_min2); \ _min1 < _min2 ? _min1 : _min2; }) What does the 4th line do?
- blasdel 18y agoThis version omits it: http://en.wikipedia.org/wiki/C_preprocessor#Multiple_evaluation_of_side_effects http://en.wikipedia.org/wiki/C_preprocessor#Multiple_evaluat... It's pretty baffling, what's the point of checking the addresses-of for equality, and then casting it to void without assignment? It's not like it could throw an exception or anything :) The compiler would just optimize it out, so it must be there solely to cause compiler warnings, but I can't figure out any cases that it covers and the final statement does not.
- deleted 18y ago[deleted]
- yan 18y agoI believe it is to make sure that x and y are of comparable types. Comparing pointers of different types generates a warning with GCC. I can be wrong though..
- babo 18y agoYou are right, it's a compile time argument checking for type equality.
- deleted 18y ago[deleted]
- blasdel 18y agoThe real oddity of this macro is the parentheses around the block, which cause it to behave much like an inline function (but without having to wrestle the type system).
- deleted 18y ago[deleted]
- deleted 18y ago[deleted]
- yan 18y agoIt's also cleaner than the do { ... } while(0) business, imho.
- tspiteri 18y agodo { ... } while(0) is Ansi C and has type void. ({ ... }) is a GCC extension and its value is the value of the last expression inside the block. So they have different uses.
- davidw 18y agoWith and without it, trying to compile this: long a = 10; int b = 20; printf("min is %ld\n", min(a, b)); With, it generates a warning: davidw@fortrock:/tmp$ gcc -o min min.c min.c: In function ‘main’: min.c:13: warning: comparison of distinct pointer types lacks a cast Without, there is no warning.
- davidw 18y agoThis is the sort of thing I'd love to see more of, rather than yet another article on the car companies.
- kiter007 18y agoSubmit good links. Vote judiciously. Don't pollute threads by whining.
- jrockway 18y agoFor someone that has only had an account for an hour (!), you sure have a lot of thoughts on how the site works. Consider this -- voting makes the articles about car companies have 84 upvotes instead of 85. Making a comment (or "whining", as you call it) can influence others to not vote-up the articles about car companies, and it can encourage others to submit better articles. So there is good reason to "whine" instead of silently not clicking the upvote button.
- kiter007 18y agoYou've made just 1 submission in 558 days? Way to contribute!
- GHFigs 18y agoPlease do not attempt to justify obnoxious and off-topic comments. The hypothetical positive effect of influencing other submitters who may or may not even be reading the story you post them to does not outweigh the very real negative effect of degrading discussion. Might I suggest as an alternative that if you wish to whine/encourage others to submit better articles, you can simply "Leave url blank to submit a question for discussion." In that way, you can create a more suitable venue for airing your grievances, rather than shitting up every other thread with either complaints about the that article or in this case, complaints about a whole class of other articles. That the highest-voted comment on this article is completely off-topic isn't and indicator of efficacy at encouraging better submissions as much as it is an indicator that the quality of the discussion is dreadful. And that, frankly, is a more dire concern for the vitality of a site than whether any given reader (even one with vague claims to seniority) has to ignore a few submissions that others have chosen not to ignore.
- streblo 18y agoI don't understand how half of this stuff works, but it's always awesome to see the guts of a complex system. The first time I looked at linux source code was comparable to the first time I looked under the hood of a car - I had only the slightest idea as to what might have been going on, but my curiosity was peaked and it made me want to learn more.