我正在用C編寫一個使用win32 API的應用程序。 當我嘗試放大我的數組的大小時,使用HeapRealloc()函數,它會更改數組中的當前值,而不是複製它們。 我使用的代碼重新分配內存:爲什麼Win32 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;
}
}
這裏是陣列的狀態,在斷點時。 饋送數組顯示當前數組狀態。 temp數組顯示新的重新分配的數組狀態(這是不同的)。
飼料數組:
feedArray http://www.freeimagehosting.net/uploads/526b0b2172.jpg
臨時數組:
tempArray http://www.freeimagehosting.net/uploads/17858f2e7e.jpg
請幫助..:\
鏈接的功能說明上MSDN
我似乎誤導了你,所以我添加了完整的相關代碼。也增加了截圖。 謝謝大家回覆:) – idcman 2009-11-19 19:57:21