博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WIN API当中的堆管理,虚拟内存及常规复制,移动,填充代码
阅读量:5991 次
发布时间:2019-06-20

本文共 4828 字,大约阅读时间需要 16 分钟。

代码一个一个的输入,有点累,但也充实。

感觉收获较多。

特别是书中将C标准库的malloc最终调用的是HeapAlloc函数。

而相对于堆内存管理负责的HeapAlloc(GlobalAlloc,LocalAlloc),属于虚拟内存管理范围的VirtualAlloc更底层。

这对理解操作系统实现及以后的软件性能及内存泄漏调度,更有帮助。

Heap.c

复制代码
1 #include 
2 #include
3 4 #define MEM_BLOCK_SIZE 32 5 6 BOOL ShowMemContent(LPVOID, SIZE_T); 7 int main(void) 8 { 9 HANDLE hHeap = GetProcessHeap();10 LPVOID lpSrc;11 LPVOID lpDis;12 13 lpSrc = HeapAlloc(hHeap, 0, MEM_BLOCK_SIZE);14 lpDis = HeapAlloc(hHeap, 0, MEM_BLOCK_SIZE);15 16 printf("HeapAlloc distribut but not make zero: \n");17 ShowMemContent(lpDis, MEM_BLOCK_SIZE);18 19 ZeroMemory(lpDis, MEM_BLOCK_SIZE);20 printf("ZeroMemory make zero: \n");21 ShowMemContent(lpDis,MEM_BLOCK_SIZE);22 23 FillMemory(lpSrc, MEM_BLOCK_SIZE, 0xBB);24 FillMemory(lpSrc, MEM_BLOCK_SIZE/2, 0xAA);25 CopyMemory(lpDis, lpSrc, MEM_BLOCK_SIZE);26 27 printf("FillMemory fill Memory: \n");28 ShowMemContent(lpDis,MEM_BLOCK_SIZE);29 30 HeapFree(hHeap, 0, lpSrc);31 HeapFree(hHeap, 0, lpDis);32 return 0;33 }34 35 BOOL ShowMemContent(LPVOID lpMem, SIZE_T dwSize)36 {37 BYTE lpShow[MEM_BLOCK_SIZE];38 INT i;39 40 if(dwSize > MEM_BLOCK_SIZE)41 {42 printf("over-flow!");43 return FALSE;44 }45 46 CopyMemory((LPVOID)lpShow, lpMem, dwSize);47 for(i = 0; i < dwSize; i++)48 {49 printf("%.2x",lpShow[i]);50 if(!((i+1)%16))51 {52 printf("\n");53 }54 }55 printf("\n");56 return TRUE;57 }
复制代码

Virtual.c

复制代码
1 #include 
2 #include
3 4 int main(void) 5 { 6 SIZE_T sizeVirtual = 4000; 7 LPVOID lpRound = (LPVOID)0x100000FF; 8 MEMORY_BASIC_INFORMATION mbi; 9 10 LPVOID lpAddress = VirtualAlloc(11 lpRound, sizeVirtual,12 MEM_COMMIT|MEM_RESERVE, PAGE_READWRITE13 );14 if(lpAddress == NULL)15 {16 printf("VirtualAlloc error: %d\n",GetLastError());17 return 1;18 }19 printf("Alloc:MEM_COMMIT|MEM_RESERVE\n");20 CopyMemory(lpAddress, "Hello", lstrlen("Hello"));21 printf("Alloction,Copy sucess.address: 0x%.8x, content: %s\n",lpAddress, lpAddress);22 VirtualQuery(lpAddress, &mbi, sizeof(mbi));23 printf("Information from VirtualQuery: \n"24 "BaseAddress:0x%.8x\tAllocationBase:0x%.8x\t"25 "AllocationProtect:0x%.8x\tRegionSize:%u\t"26 "State:0x%.8x\tProtect:0x%.8x\tType:0x%.8x\n",27 mbi.BaseAddress,mbi.AllocationBase,28 mbi.AllocationProtect,mbi.RegionSize,29 mbi.State,mbi.Protect,mbi.Type30 );31 32 printf("Free: DECOMMIT\n");33 if(!VirtualFree(lpRound, sizeVirtual, MEM_DECOMMIT))34 {35 printf("VirtualFree error: %d",GetLastError());36 return 1;37 }38 39 VirtualQuery(lpAddress, &mbi, sizeof(mbi));40 printf("Information from VirtualQuery: \n"41 "BaseAddress:0x%.8x\tAllocationBase:0x%.8x\t"42 "AllocationProtect:0x%.8x\tRegionSize:%u\t"43 "State:0x%.8x\tProtect:0x%.8x\tType:0x%.8x\n",44 mbi.BaseAddress,mbi.AllocationBase,45 mbi.AllocationProtect,mbi.RegionSize,46 mbi.State,mbi.Protect,mbi.Type47 );48 49 printf("Free:RELEASE\n");50 if(!VirtualFree(lpAddress, 0, MEM_RELEASE))51 {52 printf("VirtualFree error: %d",GetLastError());53 return 1;54 }55 return 0;56 }
复制代码

MemOp.c

复制代码
1 #include 
2 #include
3 4 #define MEM_BLOCK_SIZE 32 5 6 BOOL ShowMemContent(LPVOID, SIZE_T); 7 int main(void) 8 { 9 HANDLE hHeap = GetProcessHeap();10 LPVOID lpSrc;11 LPVOID lpDis;12 13 lpSrc = HeapAlloc(hHeap, 0, MEM_BLOCK_SIZE);14 lpDis = HeapAlloc(hHeap, 0, MEM_BLOCK_SIZE);15 16 printf("HeapAlloc distribut but not make zero: \n");17 ShowMemContent(lpDis, MEM_BLOCK_SIZE);18 19 ZeroMemory(lpDis, MEM_BLOCK_SIZE);20 printf("ZeroMemory make zero: \n");21 ShowMemContent(lpDis,MEM_BLOCK_SIZE);22 23 FillMemory(lpSrc, MEM_BLOCK_SIZE, 0xBB);24 FillMemory(lpSrc, MEM_BLOCK_SIZE/2, 0xAA);25 CopyMemory(lpDis, lpSrc, MEM_BLOCK_SIZE);26 27 printf("FillMemory fill Memory: \n");28 ShowMemContent(lpDis,MEM_BLOCK_SIZE);29 30 HeapFree(hHeap, 0, lpSrc);31 HeapFree(hHeap, 0, lpDis);32 return 0;33 }34 35 BOOL ShowMemContent(LPVOID lpMem, SIZE_T dwSize)36 {37 BYTE lpShow[MEM_BLOCK_SIZE];38 INT i;39 40 if(dwSize > MEM_BLOCK_SIZE)41 {42 printf("over-flow!");43 return FALSE;44 }45 46 CopyMemory((LPVOID)lpShow, lpMem, dwSize);47 for(i = 0; i < dwSize; i++)48 {49 printf("%.2x",lpShow[i]);50 if(!((i+1)%16))51 {52 printf("\n");53 }54 }55 printf("\n");56 return TRUE;57 }
复制代码

运行图:

转载地址:http://rjilx.baihongyu.com/

你可能感兴趣的文章
oracle技术之oracle归档模式下的Rman备份集在异机恢复
查看>>
ASM 磁盘、目录的管理
查看>>
Oracle 11g RAC CRS-4535/ORA-1507
查看>>
Javascript你必须要知道的面试题
查看>>
讲英文直接变中文!微软口译技术大突破
查看>>
Shell多线程编程的实例
查看>>
我的友情链接
查看>>
C#中判断系统的网络连接状态的方法
查看>>
xenserver Pool和Host损坏后的灾难恢复(HBA存储)
查看>>
EditPlus自动补全、模板配置
查看>>
Servlet中两个Init()的区别
查看>>
汇编中的溢出问题
查看>>
Tomcat的安装配置与JavaWeb入门教程
查看>>
谁是凶手
查看>>
Java设计模式系列之单例模式
查看>>
Nginx 配置https证书
查看>>
阿里nas挂载错误
查看>>
一些常用软件的静默安装参数(nsis,msi,InstallShield,Inno)
查看>>
【ipc-mq】根据mq的key查看使用进程
查看>>
【转】前端必读:浏览器内部工作原理
查看>>