- 论坛徽章:
- 0
|
本帖最后由 wzy9854 于 2012-02-23 13:34 编辑
int appendstring ( char** pDest, const char* pSrc )
{
int iEnd;
char *pString ;
pString = *pDest;
if ( pSrc == (char*)NULL || strlen ( pSrc ) == 0 )
{
return ;
}
/* check src */
if ( pString == (char*)NULL )
{
pString = (char*) malloc ( strlen(pSrc)+10 );/* allocate and copy */
iEnd = 0;
}
else
{
iEnd = strlen(pString );
pString = malloc(iEnd+strlen(pSrc)+10);
memcpy(pString,*pDest,iEnd);
pString[iEnd]=0;
free(*pDest);
*pDest = NULL;
}
if(pString !=NULL)
{
memcpy(pString+iEnd,pSrc,strlen(pSrc));
pString[strlen(pSrc)+iEnd]=0;
*pDest = pString;
}
return ;
}
int main(void)
{
char *buf = "abc" ;
appendstring(&buf,"d");
printf("%s",buf);
}
这是一个拼接字符串的函数,为什么free之后 会造成非法内存访问呢?悉心求教! |
|