代码原则:c/c++

来源:互联网 发布:美国2016年7月非农数据 编辑:程序博客网 时间:2024/05/17 01:31

头文件

# include<iostream># include“stdlib.h” //exitusing namespace std;# define TRUE 1# define FALSE 0# define ERROR 0# define OK 0

assert

  • assert
// 初始参数是否在有效范围内
  • 参数超出范围
// 不在范围内if(i<1||i>L.length)return ERROR...return OK

错误代码

  • malloc/free分配失败
//存储分配失败malloc/free if(!L.elem)  exit(OVERFLOW);
  • new/ delete 分配失败
//new delete 分配失败http://blog.csdn.net/water_cow/article/details/20938075int* p = new int[SIZE];if ( p == 0 ) // 检查 p 是否空指针return -1;// 其它代码 try {            int* p = new int[SIZE];            // 其它代码        } catch ( const bad_alloc& e ) {            return -1;        }//不抛出异常的new# include <iostream>using namespace std;int main(){        int * i = NULL;        i = new(nothrow) int[1000];        if (i == NULL)        {                cout << "bad allocation" << endl;                return -1;        }        return 0;}

  • const
    -将变量或者方法声明为不可变
    http://blog.csdn.net/mijian1207mijian/article/details/50196921

  • 模板
0 0