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

eliminate the side effect of Micro in C

工作和技术 crifan 1639浏览 0评论

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

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
79 queries in 0.172 seconds, using 22.08MB memory