Chinaunix首页 | 论坛 | 博客
  • 博客访问: 31097
  • 博文数量: 22
  • 博客积分: 780
  • 博客等级: 军士长
  • 技术积分: 230
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-08 17:48
文章分类

全部博文(22)

文章存档

2010年(8)

2009年(14)

我的朋友
最近访客

分类: C/C++

2009-12-30 17:51:15

(1)Do not Mistake operators of C with your poor Maths

I want to define system usable Maxium Vlan number(Its Max Number  is 4095),I found the definition in some head file:

     #ifndef MAX_VLAN_ID
     #define MAX_VLAN_ID 2^12-1
     #endif

It puzzles me when first look,because I simply use with if (u4IntfIndex > VLAN_MAX_VLAN_ID) .....
of course It runs wrong,but not everyone I asked can esaily pick it up.

(2)Do not free NULL pointer again.....

    if (!PtrRetParms)
    { 
        free(PtrRetParms);
          ...... ..........
     }

HOPE YOU NEVER SILLY DO THIS LIKE ME!!!
when system segment fault ,everyone may undergo run out of themself,"Ooops,damned the fxxx memory",keep in mind that not every system or library  is good-building,you CANNOT  lay MAY  on others,I do encounter a system library free not  check if pointer is NULL,. If you think this is only a subtle problem,then look this :
you define a global pointer somewhere char *buff;
you malloc and free memory using buff
like  this:
 
 
  if (buffer !=NULL)
            free(buffer);
  buffer = (char*)malloc(MAX_IMAGE_DOWNLOAD);
.....................
         free(buffer);

call this function twice,see what unexpected present you got!!!!
NO library  promise you that free a pointer will
make the pointer NULL:)
(3)Endian

UINT4 u4Localnum = 0;
you call a function like: 
 if( GetPortTypeId(&u4Localnum )==SUCCESS)
{
          ............
}
But if the prototype of your function is INT1 GetPortTypeId(UINT1 *pu1RetVal); in Big-Endian System,you may never get a right value!!

(4)NULL not always means nothing to do

consider an user-defined string struct
     typedef struct STRING_TYPE {
                  UINT1  *pu1_OctetList;
                  INT4   i4_Length;
                  } tSTRING_TYPE;
If you want to compare the two string,you only need write a function called  CompareUserOctetString like :
INT4   CompareUserOctetString (tSTRING_TYPE * pFirstOctetStr,
                        tSTRING_TYPE * pSecondOctetStr)
{
    if (pFirstOctetStr->i4_Length > pSecondOctetStr->i4_Length)
    {
          return GREATER;
    }
    else if (pFirstOctetStr->i4_Length < pSecondOctetStr->i4_Length)
    {
          return LESSER;
    }
      ................
}
Then the clever you simply write a second wrap function Copy second string to first ,you write like
INT4   CoopyUserOctetString (tSTRING_TYPE * pFirstOctetStr,
                        tSTRING_TYPE * pSecondOctetStr)
{
memcpy(pFirstOctetStr->pu1_OctetList, pSecondOctetStr->pu1_OctetList, pSecondOctetStr->i4_Length);
..................
}
 
NOT ASK ME WHAT IS WRONG WITH THIS,Think before you do,Check before you use,Defensive programming is a good friend through your hard programmer life.
 
(5)Function implementation VS Function ProtoType
 
Think you got a function whose prototype is int Funs(int a)
But if you call it simply like Funs() directly int some codesnip,if a bug happens,this implied bug may cost you much time to discovery why,There is no need for compiler to check this error if you not contain the function prototype in the include file.
(6)what does this setence means
#define PRINT(a) printf((a))
int a=5;
PRINT("number is %d\n",a)
阅读(185) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
请登录后评论。

登录 注册