J
Jerry
Hi,all
I write a function to copy one memory buffer to anther with
dynamic allocationg when the destination is not large enough.
The code is below:
void DSM_CC::fillBuf(unsigned char** pdstBuf,int* pdstBufBase,int*
pdstTolalLen,char* pbufToWrite,int lenToWrite)
{
unsigned char* tmp;
int step = 256;
short allocateCnt = 10;
if(*pdstBufBase + lenToWrite > *pdstTolalLen)
{
if(!(*pdstBuf))//pdstBuf == null
{
while(allocateCnt--)
{
*pdstBuf = (unsigned char*)malloc(lenToWrite + step);
if(*pdstBuf)
break;
}
*pdstTolalLen = lenToWrite + step;
}
else
{
allocateCnt = 10;
while(allocateCnt--)
{
tmp = (unsigned char*)malloc(*pdstBufBase);
if(tmp)
break;
}
memcpy(tmp,*pdstBuf,*pdstBufBase);
//\\!realloc will make the original data lost??
realloc(*pdstBuf,*pdstBufBase + lenToWrite +
step); memcpy(*pdstBuf,tmp,*pdstBufBase);
free(tmp);
*pdstTolalLen = *pdstBufBase + lenToWrite + step;
}
}
memcpy(*pdstBuf+ *pdstBufBase,pbufToWrite,lenToWrite);
*pdstBufBase += lenToWrite;
}
when the function was invoked the third time.An exception happens
at line"free(tmp)"
Anther:Is there any way to deside if a pointer is valid or not?
For example,I use if(!(*pdstBuf)) to detect it.But we know when
a pointer declaration,the initialization value may not be NULL(that's
0).SO if(!(*pdstBuf)) will not work when an invalid pointer with value
0xccccccc(VC 6.0)
::Envir0nment: vc6.0 sp6
please why? Thx!
I write a function to copy one memory buffer to anther with
dynamic allocationg when the destination is not large enough.
The code is below:
void DSM_CC::fillBuf(unsigned char** pdstBuf,int* pdstBufBase,int*
pdstTolalLen,char* pbufToWrite,int lenToWrite)
{
unsigned char* tmp;
int step = 256;
short allocateCnt = 10;
if(*pdstBufBase + lenToWrite > *pdstTolalLen)
{
if(!(*pdstBuf))//pdstBuf == null
{
while(allocateCnt--)
{
*pdstBuf = (unsigned char*)malloc(lenToWrite + step);
if(*pdstBuf)
break;
}
*pdstTolalLen = lenToWrite + step;
}
else
{
allocateCnt = 10;
while(allocateCnt--)
{
tmp = (unsigned char*)malloc(*pdstBufBase);
if(tmp)
break;
}
memcpy(tmp,*pdstBuf,*pdstBufBase);
//\\!realloc will make the original data lost??
realloc(*pdstBuf,*pdstBufBase + lenToWrite +
step); memcpy(*pdstBuf,tmp,*pdstBufBase);
free(tmp);
*pdstTolalLen = *pdstBufBase + lenToWrite + step;
}
}
memcpy(*pdstBuf+ *pdstBufBase,pbufToWrite,lenToWrite);
*pdstBufBase += lenToWrite;
}
when the function was invoked the third time.An exception happens
at line"free(tmp)"
Anther:Is there any way to deside if a pointer is valid or not?
For example,I use if(!(*pdstBuf)) to detect it.But we know when
a pointer declaration,the initialization value may not be NULL(that's
0).SO if(!(*pdstBuf)) will not work when an invalid pointer with value
0xccccccc(VC 6.0)
::Envir0nment: vc6.0 sp6
please why? Thx!