指向bool变量的指针

      struct ThreadParam
      {
        bool  *exit;
        ......
      };
     
      ......

      ThreadParam my;
      my.exit = false;

      pthread_create(thread, NULL, threadProc, &my);

      ......

      void threadProc(void* p)
      {
        ThreadParam *param = (ThreadParam*)p;
        if(param->exit)
        {
        ......

      昨天的一段程序,却发现param->exit的判断总是成功,心想怪了:我赋值为false了啊,难道my析构了?调了半天才发现是 if 判断的问题,应该判断bool的指 *(param->exit) ,判断 param->exit 就成了判断指针是否为空了(当然不为空了)!
      以前听过有人建议所有对bool量的判断应该用 if(param->exit == true) ,这样虽然麻烦,倒是可以通过编译器的类型检查提早发现上面的问题。

相关文章

分类

留言:

关于文章

This page contains a single entry by DongHao published on 10 7, 2008 1:05 PM.

cin was the previous entry in this blog.

《CFM56方程》 is the next entry in this blog.

Find recent content on the main index or look in the 存档 to find all content.