写过最恶心的bug

1. 创建bool指针赋值为false

const char *cp = "hello";    //OK
bool *flag = false;    //bug

第二句的操作时创建一个指向bool型的指针,将其赋值为false。本意是向创建一个指针,它指向的对象值为false。
这样写的问题不仅是语义不对,同时,如果写成bool *flag = true;则指针值为1,其后我对指针赋值*flag = true;相当于对一个固定地址赋值,这是非法的,没有写权限。分配地址应该由操作系统执行,不能直接对某个固定地址赋值,这样存在安全问题。

int main()
{
   
   int * a = (int*)10000;
   *a = true;

   if (*a)
   {
      printf("Test\n");
   }
   else
   {
      printf("Empty\n");
   }

   return 0;
}

在这里插入图片描述
2. 创建类对象

auto err = nierr::Status(errorCode, component, fileName, line, success);

这种写法相当于:

int temp = 1;
int a = temp;

这样写是冗余的,虽然不是bug,但是仍然恶心。
正确写法:

nierr::Statues err(errorCode, component, fileName, line, success);

3. 对一个nonecopyable类创建对象

class CLibrary : private boost::noncopyable {};

创建一个对象:

CLibrary lib = CLibrary(); //it is a delete function

合适的写法:

auto lib = std::make_shared<CLibrary>();

不可以写成:

CLibrary *lib = std::make_shared<CLibrary>(); //类型不匹配

正确的写法:

std::shared_ptr<CLibrary> lib = std::make_shared<CLibrary>();

4. 比较字符指针而不是字符串
原意想要比较两个字符串是否相等,但结构体里存储的是指向字符数组的指针,直接比较了指针,永远不相等。

typedef struct nierr_Status
{
   int32_t code;        /*!< status code */
   uint32_t capacity;   /*!< capacity of the json buffer in bytes total */
   nierr_ReallocJson *reallocJson; /*!< function pointer to realloc json
                                     buffer. Please refer to nierr_ReallocJson
                                     function type for more details.
                                     \sa nierr_ReallocJson */
   char* json;          /*!< pointer to description buffer in jsonz
                          format. Please allocate and initialize this buffer
                          using nierr_Status_jsonReserve() or
                          nierr_Status_jsonSet(). It's automatically freed by
                          nierr_Status_reset().
                          \warning Do not write to this buffer directly without
                          using jsonz library.
                          */
} nierr_Status;
EXPECT_EQ(lastError.json, expecedtError.json);

正确写法:

int equal = strcmp(lastError.json, expecedtError.json);
EXPECT_EQ(equal, 0);

5. function argument and parameter
在这里插入图片描述
argument of type “…” is incompatible with parameter of type “…”

A parameter is the variable which is part of the method’s signature (method declaration). 
An argument is an expression used when calling the method.
void Foo(int i, float f)
{
    // Do things
}

void Bar()
{
    int anInt = 1;
    Foo(anInt, 2.0);
}

Here i and f are the parameters, and anInt and 2.0 are the arguments.

  1. 宏定义大小写
TEST(socketpair, Multiple) {
#if kNIOSWindows
   nNISLSCCMN::WinsockInitializer winsockInitializer;
#endif

// We have previously observed issues around 8,100 on Phar Lap.
// So we use iteration number as 3,000 for pharlap and 20,000 for the other platforms.
#if kNIOSPharLap
   const int kCount = 3000;
#else
   const int kCount = 20000;
#endif
   for (int i = 0; i < kCount; ++i)
   {
      ASSERT_NO_THROW(ScopedSocketPair()) << "Iteration " << i;
   }
   // Open and hold max 100 socket pairs, then retest to ensure this does not cause
   // problems.
#if kNIOSPharLap    //写成了#kNIOSPharlap(小写)
   nNISLSCCMN::SocketManager socketManager;
   std::vector<boost::shared_ptr<ScopedSocketPair> > socktPairs(100);
   for (int i = 0; i < 100; ++i)
   {
      try
      {
         socketManager.validateResource();
         socktPairs[i] = boost::make_shared<ScopedSocketPair>();
      }
      catch (const nNISLSCSHAREDCMN::BasicError& err)
      {
         ASSERT_EQ(err.status.getCode(), nNISLSCERR::kErrorNoAvailableSockets);
         break;
      }
   }
#else
   ScopedSocketPair socketPairs[100];
   mNIUnused(socketPairs); // Squelch GCC warning for ScopedSocketPair array.
#endif

   for (int i = 0; i < kCount; ++i)
   {
      ASSERT_NO_THROW(ScopedSocketPair()) << "Iteration " << i;
   }
}
  1. 创建std::pair
class getPropertyNameWithValue{
public:
   std::pair<const std::string, const std::string> operator()(const std::string s1, const std::string s2) {
      return !s1.empty() ?
         std::make_pair(std::string("chassis_ip"), s1)
         :
         std::make_pair(std::string("chassis_hostname"), s2);
   }
};

在创建可调用对象时,make_pair后面无需添加模板类型make_pair<const std::string, double>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值