// windows_28_windows_heap.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <windows.h>#include <stdlib.h>void HeapInfo( ){ //默认堆句柄 HANDLE hHeap = GetProcessHeap( ); printf( "Default Heap:%p\n", hHeap ); //所有堆的句柄 HANDLE hHeaps[256] = { 0 }; DWORD nCount = GetProcessHeaps( 256, hHeaps ); printf( "All Heap:%d\n", nCount ); for (DWORD nIndex = 0; nIndex < nCount; nIndex++) { printf( "\t%d: %p\n", nIndex + 1, hHeaps[nIndex] ); }}void Heap( ){ HeapInfo( );//堆测试 //1、创建堆 //HeapCreate HANDLE hHeap = HeapCreate( HEAP_GENERATE_EXCEPTIONS, 1024 * 1024, 0 ); HeapInfo( ); //堆测试 //2、分配内存 //HeapAlloc CHAR *pszBuf = (CHAR*)HeapAlloc( hHeap, HEAP_ZERO_MEMORY, 100 ); //3、使用内存 printf( "HeapCreate:%p\n", hHeap ); printf( "HeapCreateBuf:%p\n", pszBuf ); //strcpy_s( pszBuf, ( rsize_t)strlen( "hello Heap\n" ), "hello Heap\n" ); //printf( "%s", pszBuf ); //4、释放内存 //HeapFree HeapFree( hHeap, 0, pszBuf ); //5、释放堆 //HeapDestroy HeapDestroy( hHeap ); HeapInfo( );//堆测试}int _tmain(int argc, _TCHAR* argv[]){ //调试看代码 CHAR *pszBuf = (CHAR*)malloc( 100 ); Heap( ); return 0;}