我正在使用 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 数组显示当前数组状态。临时数组显示新的重新分配的数组状态(这是不同的)。
饲料数组:
feedArray http://www.freeimagehosting.net/uploads/526b0b2172.jpg
临时数组:
tempArray http://www.freeimagehosting.net/uploads/17858f2e7e.jpg
请帮忙.. :\
链接到MSDN上的功能说明