c++内存管理基础[2]
1. /*Code By Pnig0s1992*/
2. #include <Windows.h>
3. #include <stdio.h>
4. #include <tchar.h>
5.
6. #define MEM_BLOCK_SIZE 32
7.
8. BOOL ShowMemContent(LPVOID lpMem,SIZE_T dwSize);
9.
10. int main(void){
11. HANDLE hHeap = GetProcessHeap();
12. LPVOID lpSrc;
13. LPVOID lpDis;
14. if(hHeap == NULL){
15. printf("获取当前进程中的堆错误:%d\n",GetLastError());
16. return 1;
17. }
18. lpSrc = HeapAlloc(hHeap,0,MEM_BLOCK_SIZE);
19. lpDis = HeapAlloc(hHeap,0,MEM_BLOCK_SIZE);
20.
21. printf("未清零的内存:\n");
22.
23. ShowMemContent(lpSrc,MEM_BLOCK_SIZE);
24. ShowMemContent(lpDis,MEM_BLOCK_SIZE);
25.
26. ZeroMemory(lpSrc,MEM_BLOCK_SIZE);
27. ZeroMemory(lpDis,MEM_BLOCK_SIZE);
28.
29. printf("清零后的内存:\n");
30.
31. ShowMemContent(lpSrc,MEM_BLOCK_SIZE);
32. ShowMemContent(lpDis,MEM_BLOCK_SIZE);
33.
34. FillMemory(lpSrc,MEM_BLOCK_SIZE,0xBB);
35. FillMemory(lpSrc,MEM_BLOCK_SIZE/2,0xAA);
36.
37. CopyMemory(lpDis,lpSrc,MEM_BLOCK_SIZE);
38.
39. printf("填充复制后的内存:\n");
40. ShowMemContent(lpDis,MEM_BLOCK_SIZE);
41.
42. system("pause");
43. return 0;
44. }
45.
46. BOOL ShowMemContent(LPVOID lpMem,SIZE_T dwSize){
47. BYTE lpShow[MEM_BLOCK_SIZE];
48. INT i=0;
49. if(dwSize>MEM_BLOCK_SIZE){
50. printf("over flow.");
51. return FALSE;
52. }
53. CopyMemory(lpShow,lpMem,MEM_BLOCK_SIZE);
54. for(i=0;i<dwSize;i++){
55. printf("%.2X",lpShow[i]);
56. if(!((i+1)%16)){
57. printf("\n");
58. }
59. }
60. printf("\n");
61. return TRUE;
62. }
本文出自 “Pnig0s” 博客
补充:软件开发 , Vc ,