如何處理未使用的變量

有一些函數傳入了沒有任何作用的變量,但是,這些變量在以後可能會用上(可擴展性)。

而編譯器一般會發出警告,提示該變量沒有被使用。如何處理這種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) )
明白了吧?
發佈了171 篇原創文章 · 獲贊 9 · 訪問量 22萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章