realloc
int _tmain(int argc, _TCHAR* argv[])
{
char *buf1 = NULL;
char *temp1 = NULL;
char *temp2 = NULL;
for (int i = 1 ; i<1000; i++)
{
temp1 = buf1 = (char*)realloc(buf1,1000*i);
memset(buf1,'\0',1000*i);
#if 1
if (temp1 != temp2)
{
if ( temp2 != NULL)
{
free(temp2);
temp2 = NULL;
}
temp2 = buf1;
}
#endif
}
if(buf1)
free(buf1);
buf1 = NULL;
getchar();
return 0;
}.
就是想了解realloc在扩张申请空间得不到满足的时候,是另外开辟一个连续空间,而且系统会将原来的块数据拷贝到新块里面.
这是不是处问题了?如果原来的块不用,是不是应该释放掉?我上面的程序就是这个思想来测试一下,可是一运行到红字体部分,程序就出错误,错误提示:This may be due to a corruption of the heap, and indicates a bug in gdg.exe or any of the DLLs it has loaded.
根据错误提示,可能原因是:
1. 访问已经被释放的内存
2. 再次释放已经被释放过的内存
所以不知道到底怎么回事了??