#include <Windows.h>
#include <iostream>
using namespace std;
int main()
{
// Create a private heap
HANDLE hHeapNew = HeapCreate(0, // serialized and no exception
1024*4, // the initial size of the heap
1024*16); // the maximum size of the heap
if (hHeapNew == NULL)
{
cout << "HeapCreate failed with error: " << GetLastError() << endl;
return 0;
}
// Allocate a block of memory from the private heap
LPVOID lpBuffer = HeapAlloc(hHeapNew, // heap handle
HEAP_ZERO_MEMORY, // initialized to be 0
100); // number of bytes to be allocated
if (lpBuffer == NULL)
{
cout << "HeapAlloc failed with error: " << GetLastError() << endl;
return 0;
}
sprintf((char*)lpBuffer, "Hello Kitty");
cout << (char*)lpBuffer << endl;
// Free the memory block allocated from the private heap
BOOL flag;
flag = HeapFree(hHeapNew, 0, lpBuffer);
if (!flag)
{
cout << "HeapFree failed with error: " << GetLastError() << endl;
return 0;
}
// Destroy the heap
flag = HeapDestroy(hHeapNew);
if (!flag)
{
cout << "HeapDestroy failed with error: " << GetLastError() << endl;
return 0;
}
system("pause");
return 0;
}
Heap,创建进程私有堆
最新推荐文章于 2023-04-26 22:36:02 发布