如何处理未使用的变量

有一些函数传入了没有任何作用的变量,但是,这些变量在以后可能会用上(可扩展性)。

而编译器一般会发出警告,提示该变量没有被使用。如何处理这种case呢?

业界有一行统一的代码来解决这个问题。

handler_t stat_cache_handle_fdevent(server *srv, void *_fce, int revent) {
  size_t i;
  stat_cache *sc = srv->stat_cache;
  size_t events;

  UNUSED(_fce);
  /* */

  if ((revent & FDEVENT_IN) &&
      sc->fam) {

    events = FAMPending(sc->fam);

    for (i = 0; i < events; i++) {
      FAMEvent fe;
      fam_dir_entry *fam_dir;
      splay_tree *node;
      int ndx, j;

      FAMNextEvent(sc->fam, &fe);

      /* handle event */

      switch(fe.code) {
      case FAMChanged:
      case FAMDeleted:
      case FAMMoved:
        /* if the filename is a directory remove the entry */

        fam_dir = fe.userdata;
        fam_dir->version++;

        /* file/dir is still here */
        if (fe.code == FAMChanged) break;

        /* we have 2 versions, follow and no-follow-symlink */

        for (j = 0; j < 2; j++) {
          buffer_copy_string(sc->hash_key, fe.filename);
          buffer_append_long(sc->hash_key, j);

          ndx = hashme(sc->hash_key);

          sc->dirs = splaytree_splay(sc->dirs, ndx);
          node = sc->dirs;

          if (node && (node->key == ndx)) {
            int osize = splaytree_size(sc->dirs);

            fam_dir_entry_free(node->data);
            sc->dirs = splaytree_delete(sc->dirs, ndx);

            assert(osize - 1 == splaytree_size(sc->dirs));
          }
        }
        break;
      default:
        break;
      }
    }
  }

  if (revent & FDEVENT_HUP) {
    /* fam closed the connection */
    srv->stat_cache->fam_fcce_ndx = -1;

    fdevent_event_del(srv->ev, &(sc->fam_fcce_ndx), FAMCONNECTION_GETFD(sc->fam));
    fdevent_unregister(srv->ev, FAMCONNECTION_GETFD(sc->fam));

    FAMClose(sc->fam);
    free(sc->fam);

    sc->fam = NULL;
  }

  return HANDLER_GO_ON;
}
其中的:
UNUSED(_fce);
就是用来处理这种情况。

UNUSED宏定义如下:

#define UNUSED(x) ( (void)(x) )
明白了吧?
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值