28 windows_28_windows_heap 堆

windows_28_windows_heap 堆


  1. // windows_28_windows_heap.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include <windows.h>
  5. #include <stdlib.h>
  6. void HeapInfo( )
  7. {
  8. //默认堆句柄
  9. HANDLE hHeap = GetProcessHeap( );
  10. printf( "Default Heap:%p\n", hHeap );
  11. //所有堆的句柄
  12. HANDLE hHeaps[256] = { 0 };
  13. DWORD nCount = GetProcessHeaps( 256, hHeaps );
  14. printf( "All Heap:%d\n", nCount );
  15. for (DWORD nIndex = 0; nIndex < nCount; nIndex++)
  16. {
  17. printf( "\t%d: %p\n", nIndex + 1, hHeaps[nIndex] );
  18. }
  19. }
  20. void Heap( )
  21. {
  22. HeapInfo( );//堆测试
  23. //1、创建堆
  24. //HeapCreate
  25. HANDLE hHeap = HeapCreate( HEAP_GENERATE_EXCEPTIONS, 1024 * 1024, 0 );
  26. HeapInfo( ); //堆测试
  27. //2、分配内存
  28. //HeapAlloc
  29. CHAR *pszBuf = (CHAR*)HeapAlloc( hHeap, HEAP_ZERO_MEMORY, 100 );
  30. //3、使用内存
  31. printf( "HeapCreate:%p\n", hHeap );
  32. printf( "HeapCreateBuf:%p\n", pszBuf );
  33. //strcpy_s( pszBuf, ( rsize_t)strlen( "hello Heap\n" ), "hello Heap\n" );
  34. //printf( "%s", pszBuf );
  35. //4、释放内存
  36. //HeapFree
  37. HeapFree( hHeap, 0, pszBuf );
  38. //5、释放堆
  39. //HeapDestroy
  40. HeapDestroy( hHeap );
  41. HeapInfo( );//堆测试
  42. }
  43. int _tmain(int argc, _TCHAR* argv[])
  44. {
  45. //调试看代码
  46. CHAR *pszBuf = (CHAR*)malloc( 100 );
  47. Heap( );
  48. return 0;
  49. }





posted @ 2016-06-10 12:01  -刀狂剑痴-  阅读(201)  评论(0编辑  收藏  举报