18_Heap.cpp
演示如何使用堆
18_Heap.cpp
演示如何使用堆
文章目录
/*------------------------------------------------------------------------
18_Heap.cpp
演示如何使用堆
-----------------------------------------------------------------------*/
#include <tchar.h>
#include <windows.h>
#include <time.h>
#include <stdio.h>
void PrintArray(float fAarry[],int iCnt)
{
for (int i = 0; i < iCnt; i++)
printf(“[%03d]=%0.0f\t”, i, fAarry[i]);
return;
}
int _tmain()
{
srand((unsigned)time(NULL));
//在进程默认堆中申请内存
HANDLE hHeap = GetProcessHeap();
const int iCnt = 200;
//申请fAarry内存块
float* fArray = (float*)HeapAlloc(hHeap, HEAP_ZERO_MEMORY, iCnt * sizeof(float));
for (int i = 0; i < iCnt; i++)
fArray[i] = 1.0f * rand();
//扩大fArry内存块的大小
fArray = (float*)HeapReAlloc(hHeap, HEAP_ZERO_MEMORY, fArray, 2 * iCnt * sizeof(float));
for (int i = 0; i < 2*iCnt; i++)
fArray[i] = 1.0f * rand();
//打印内存块的内容
printf("Default Heap:\n");
PrintArray(fArray, 2 * iCnt);
//释放堆
HeapFree(hHeap, 0, fArray);
//创建私有堆
hHeap = HeapCreate(HEAP_GENERATE_EXCEPTIONS, 0, 0);
//申请内存块
fArray = (float*)HeapAlloc(hHeap, HEAP_ZERO_MEMORY, iCnt * sizeof(float));
for (int i = 0; i < iCnt; i++)
fArray[i] = 1.0f * rand();
//扩大fArry内存块的大小
fArray = (float*)HeapReAlloc(hHeap, HEAP_ZERO_MEMORY, fArray, 2 * iCnt * sizeof(float));
for (int i = 0; i < 2 * iCnt; i++)
fArray[i] = 1.0f * rand();
//打印内存块的内容
printf("Additional Heap:\n");
PrintArray(fArray, 2 * iCnt);
HeapFree(hHeap, 0, fArray);
//私有堆需要手动释放
HeapDestroy(hHeap);
_tsystem(_T("pause"));
return 0;
}
标签:iCnt,float,Windows,18,编程,hHeap,int,Heap,fArray From: https://blog.csdn.net/zhyjhacker/article/details/141474613