zoukankan      html  css  js  c++  java
  • My Trap For C++

    1 My Trap For C++

    Discovery my fauls in my habit and overcome them.

    1.1 Initialize variable

    In 2012, at about 1:00am, I still worked in company, suddenly other staff discovevied crashes in server. I checked it, crashes occured when container's iterator isn't initialized; because of mass copying-code, the bugs appears in many places. I rapidly completed all the variable initializaiton.

    From then on, when a variable is declared, I must initialize it.

    Gernerally, we initialize different types as below:

    general type

    int a = 0;
    double a = 0.0;
    char a = '';
    

    general type's array

    int a[10] = {0};
    double f[10] = {0.0};
    

    pointer

    int* p_a = NULL;
    std::vector<int>* p_a = NULL;
    

    class

    class Object {
     public:
      Object():a(0), b(0.0), c('') {
      }
     private:
      int a;
      double b;
      char c;
    };
    

    Certainly you can initialize your variable with other value instead of default value above when your variable is not a undefilized variable.

    1.2 Remove when make iteration to container

    When making iteration to container, you are removing your container's elements.

    for (iter = c.begin(); c.end() != iter; ++iter) {
      if (xxx) {
        erase(iter);
      }
    }
    

    Obviously, you can find out the bug as soon as possible. When a function with removing the container's element is called in the "for" body, it isn't obvious for you.

    So when crashes appear during the iteration to container, you should check the "for" body carefully.

    I list the method of doing iteration when remove elements in the containers as below:

    vector, list

    std::vector<int>::iterator iter = v.begin();
    while (v.end() != iter) {
      if (xxx) {
        iter = v.erase(iter);
      } else {
        ++iter;
      }
    }
    

    map, set, multimap, multiset

    std::map<int>::iterator iter = rbtree.begin();
    while (rbtree.end() != iter) {
      if (xxx) {
        v.erase(iter++);
      } else {
        ++iter;
      }
    }
    

    To know all kind of the container's "erase" function, you can refer the website:

    http://www.cplusplus.com

    1.3 Forget increment or decrement when use "while"

    In the recent past, I use mass "while" without increment or decrement, make mass infinite loop. At that time, I lose the faith to myself. I replace "while" with "for" in my code as possible.

    for(std::hash_map<int, std::vector<int> >::iterator iter = m.begin(); m.end() != iter; ++iter) {
    //TODO
    }
    

    Obviousely "for" style reminds me to add increment or decrement. But "for" style's condition is too long, you can break the line and "while" style is cleaner. Now I can overcome the problem, I firstly implement a "while" frame with increment or decrement, then add my code in the "TODO" position.

    std::hash_map<int, std::vector<int> >::iterator iter = m.begin();
    while (m.end() != iter) {
      //TODO
      ++iter;
    }
    

    Tips: When add "continue" in the "TODO" position, after execute the "contine" command, "while" don't make indecrement or decrement, but "for" make it.

  • 相关阅读:
    Linux中的官方源、镜像源汇总
    提示"libc.so.6: version `GLIBC_2.14' not found"
    win8.1 安装msi软件出现 2503、2502
    平均负载(Load average)
    oracle 导入报错:field in data file exceeds maximum length
    一个命令的输出作为另外一个命令的输入
    Http 状态码
    Linux 命令总结
    ORA-12505: TNS: 监听程序当前无法识别连接描述符中所给出的SID等错误解决方法
    轻松应对IDC机房带宽突然暴涨问题
  • 原文地址:https://www.cnblogs.com/wind-qu/p/3800766.html
Copyright © 2011-2022 走看看