代码一个一个的输入,有点累,但也充实。
感觉收获较多。
特别是书中将C标准库的malloc最终调用的是HeapAlloc函数。
而相对于堆内存管理负责的HeapAlloc(GlobalAlloc,LocalAlloc),属于虚拟内存管理范围的VirtualAlloc更底层。
这对理解操作系统实现及以后的软件性能及内存泄漏调度,更有帮助。
Heap.c
1 #include <Windows.h> 2 #include <stdio.h> 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 <Windows.h> 2 #include <stdio.h> 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_READWRITE 13 ); 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.Type 30 ); 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.Type 47 ); 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 <Windows.h> 2 #include <stdio.h> 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 }
运行图:
时间: 2024-09-11 10:39:46