- 论坛徽章:
- 0
|
我今天看到一个文章,说"FreeMsgBuffer(unsigned char* msg)"其实没有真正释放内存,或者说用"pszTmp == NULL"判断是不正确的,但后面的"define FreeMsgBuffer(msgBuff) "就可以了
能帮忙解释再透彻些吗?
#include <stdio.h>
#include <stdlib.h>
#if 0
void FreeMsgBuffer(unsigned char* msg)
{
if (NULL != msg)
{
free(msg);
msg = NULL;
}
}
#else
#define FreeMsgBuffer(msgBuff) do\
{\
if(NULL != msgBuff)\
{\
free(msgBuff);\
msgBuff = NULL;\
}\
}while(0 != msgBuff)
#endif
void main(void)
{
char *pszTmp;
pszTmp = NULL;
pszTmp = (char *)malloc(sizeof(char) * 10);
strcpy(pszTmp, "leman");
printf("name: %s\n", pszTmp);
FreeMsgBuffer(pszTmp);
if (pszTmp == NULL)
{
printf("已释放\n");
}
else
{
printf("内存内容: %d\n", pszTmp);
}
} |
|