我正在使用win32 API用C语言编写一个应用程序。 当我尝试使用HeapRealloc()函数扩大数组的大小时,它会改变数组中当前的值,而不是复制它们。 我用来重新分配内存的代码:
BOOL ChangeFeedArraySize(UINT newSize)
{
char tempChar[20] = "";
PFEED tempArr;
if (newSize == 1)
{
tempArr = (PFEED)HeapAlloc(heap, HEAP_ZERO_MEMORY, sizeof(FEED));
}
else
{
tempArr = (PFEED)HeapReAlloc(heap, HEAP_ZERO_MEMORY, categoryArray, newSize * sizeof(FEED));
// FEED - a struct
// PFEED - a pointer to the struct
// categoryArray - array to be reallocated
}
if (tempArr != NULL)
{
MessageBox(NULL, ltoa(HeapSize(heap, 0, tempArr),tempChar,10) , "Heap size after reallocation", MB_OK | MB_ICONEXCLAMATION);
feedArray = tempArr;
return TRUE;
}
else
{
return FALSE;
}
}
以下是调试时数组的状态。 feed数组显示当前数组状态。 temp数组显示重新分配后的新数组状态(不同于原来的数组)。
feed数组: feedArray http://www.freeimagehosting.net/uploads/526b0b2172.jpg temp数组: tempArray http://www.freeimagehosting.net/uploads/17858f2e7e.jpg 请帮忙.. :\
函数描述链接在MSDN。