小编idc*_*man的帖子

为什么Win32 HeapReAlloc()会改变值?

我正在使用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 …
Run Code Online (Sandbox Code Playgroud)

c heap winapi memory-management realloc

3
推荐指数
1
解决办法
1626
查看次数

标签 统计

c ×1

heap ×1

memory-management ×1

realloc ×1

winapi ×1