Liuw's Thinkpad

想要赢就先学会输,想要成功就先学会失败

Archive for the ‘type’ tag

Type-checking in gcc

without comments

In linux/kernel.h, there is such a macro

/*
 * min()/max() macros that also do
 * strict type-checking.. See the
 * "unnecessary" pointer comparison.
 */
#define min(x,y) ({ \
        typeof(x) _x = (x); \
        typeof(y) _y = (y); \
        (void) (&_x == &_y); \
        _x < _y ? _x : _y; })

In order to do strict type-checking, that “unnecessary” pointer comparison is, however, necessary. If we compare two distinct pointers without a cast, gcc will complain about it, so that we are able to figure out where things go wild.

To go further, we need some knowledge about Type System, which means “a tractable syntactic method for proving the absence of certain program behaviors by classifying phrases according to the kinds of values they compute”. That requires an understanding of computer language theories, which is beyond my knowledge now. I shall figure it out in the future.

http://en.wikipedia.org/wiki/Type_system

Written by liuw

December 7th, 2009 at 4:09 pm