Why does Win32 HeapReAlloc () change values?

I am writing a C application using the win32 API. When I try to increase the size of my array using the HeapRealloc () function, it changes my current values ​​in the array, rather than copying them. The code I'm using to reallocate memory is:

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;
    }
}

Here is the status of the arrays when at a breakpoint. The feed array shows the current state of the array. the temp array shows the new state of the redistributed array (which is different).

feed array:

feedArray http://www.freeimagehosting.net/uploads/526b0b2172.jpg

temp array:

tempArray http://www.freeimagehosting.net/uploads/17858f2e7e.jpg

Please, help..:\

Feature Description Link on MSDN

+3
4

, Windows, realloc(), .

, , , , , . , .

, , ? , , , . , , , .

, , , :

void *newmem = realloc(oldmem, newsize);
if (!newmem)
{
   // TODO: handle failure
   // possibly free(oldmem); depending on how you want to handle errors
}
else
{
   oldmem = newmem;
}

, , "oldmem = realloc(oldmem, newsize);", , , oldmem, .

:

, , - :

if (newSize == 1)
{
    tempArr = (PFEED)HeapAlloc(heap, HEAP_ZERO_MEMORY, sizeof(FEED));
}

, -, , . , if (feedArray == NULL), newSize * sizeof(FEED)?

:

OK. , , :

    tempArr = (PFEED)HeapReAlloc(heap, HEAP_ZERO_MEMORY, categoryArray,
                                 newSize * sizeof(FEED));
    // Snip...

    if (tempArr != NULL)
    {
        // Snip...
        feedArray = tempArr;

.

+6

, :

HeapReAlloc , . , .

, : , ? ? , / , ( , , , , , , , ).

+2

HeapReAlloc Array , . , . tempArr:

tempArr = (PFEED)HeapReAlloc(heap, HEAP_ZERO_MEMORY, categoryArray, newSize * sizeof(FEED)); 
cetagoryArray = tempArr;
+2

Where are you looking for a call to HeapRealloc? you should look for a RETURNED pointer , not the original one that was released after your call

0
source

Source: https://habr.com/ru/post/1723391/


All Articles