最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

eliminate the side effect of Micro in C

工作和技术 crifan 1936浏览

use this (they use two local variable):

#define min_t(type,x,y) ({ type __x=(x); type __y=(y); __x<__y?__x:__y; })

instead of:

#define min(x,y) ( (x) < (y) ? (x) : (y); )

because ,the second one hase side effect when do like this :

min(++ia, ++ib) –> ( (++ia) < (++ib) ? (++ia) : (++ib) )

has the side effect : the input Micro be ++ twice !

also,we can use this one :

#define min(x,y) ({       

const typeof(x) _x=(x);

const typeof(y) _y=(y);

(void) (&_x=&_y);        

_x < _y ? _x : _y;})

the function of typeof is to get the type of the input variable.and ,the (void) (&_x=&_y); is to check whether the two valiables’ type is the same .

digest from:《Linux设备驱动开发详解》 宋宝华 编著

转载请注明:在路上 » eliminate the side effect of Micro in C

76 queries in 0.144 seconds, using 19.20MB memory