最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

eliminate the side effect of Micro in C

工作和技术 crifan 613浏览 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,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
55 queries in 0.114 seconds, using 18.84MB memory