日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

WIN API当中的堆管理,虚拟内存及常规复制,移动,填充代码

發(fā)布時間:2025/4/16 66 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WIN API当中的堆管理,虚拟内存及常规复制,移动,填充代码 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

代碼一個一個的輸入,有點累,但也充實。

感覺收獲較多。

特別是書中將C標準庫的malloc最終調(diào)用的是HeapAlloc函數(shù)。

而相對于堆內(nèi)存管理負責(zé)的HeapAlloc(GlobalAlloc,LocalAlloc),屬于虛擬內(nèi)存管理范圍的VirtualAlloc更底層。

這對理解操作系統(tǒng)實現(xiàn)及以后的軟件性能及內(nèi)存泄漏調(diào)度,更有幫助。

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_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.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 }

運行圖:

《新程序員》:云原生和全面數(shù)字化實踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的WIN API当中的堆管理,虚拟内存及常规复制,移动,填充代码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。