memset的使用:
(1)char *pString = new char[15];
memset(pString, 0x00, 15*pString);
(2)char szString[15];
memset(szString, 0x00, sizeof(szString));
(3)struct TStructTest
{
char szNbr[10];
int nServId;
long nCustId;
};
TStructTest tStructTmp;
memset(&tStructTmp, 0x00, sizeof(tStructTmp));
(4)TStructTest tStructGroup[5];
memset(&tStructGroup[0], 0x00, sizeof(tStructGroup));
或者 memset(&tStructGroup[0], 0x00, 5*sizeof(TStructGroup));
(5)TStructTest pStructGroup = new pStructGroup[10];
memset(pStructGroup, 0x00, 10*pStructGroup);
(6)typedef struct Test_
{
int *p;
}Test;
int main(int argc, char** argv)
{
Test* test = (Test *)malloc(sizeof(Test));
test->p = (int *)malloc(sizeof(int));
*(test->p) = 2;
free(test->p);
free(test);
memset(test, 0, sizeof(Test));//将指针置为NULL
*(test->p) = 3;
return 0;
}
(7)如果结构体里含有vector,string等容器,不能使用memset对结构体进行初始化,否则core dump。