设为首页 加入收藏

TOP

c++内存管理基础[2]
2014-11-23 21:30:26 】 浏览:510
Tags:内存 管理 基础

 
 

1. /*Code By Pnig0s1992*/

2. #include

3. #include

4. #include

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

55. printf("%.2X",lpShow[i]);

56. if(!((i+1)%16)){

57. printf("\n");

58. }

59. }

60. printf("\n");

61. return TRUE;

62. } \

本文出自 “Pnig0s” 博客

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇VC读取ini文件 下一篇VC++中的自定义消息

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目