天天看點

記憶體管理基礎[1]

#include <stdio.h> 

#include <Windows.h> 

#include <tchar.h> 

/*Code By Pnig0s1992*/ 

int main(void){ 

    SIZE_T sizeVirtual = 4000; 

    LPVOID lpRound = (LPVOID)0x100000FF; 

    MEMORY_BASIC_INFORMATION mbi; 

    LPVOID lpAddress = VirtualAlloc(lpRound,sizeVirtual,MEM_COMMIT|MEM_RESERVE,PAGE_READWRITE); 

    if(lpAddress == NULL){ 

        printf("記憶體配置設定失敗:%d\n",GetLastError()); 

        return 1; 

    } 

    printf("Alloc:MEM_COMMIT|MEM_RESERVE\n"); 

    CopyMemory(lpAddress,"pnig0s1992",strlen("pnig0s1992")); 

    printf("配置設定,複制成功,位址:0x%.8x\n内容:%s\n",lpAddress,lpAddress); 

    VirtualQuery(lpAddress,&mbi,sizeof(mbi)); 

    printf("使用VirtualQuery()獲得資訊:\n"); 

    printf("BaseAddress:0x%.8x\tAllocationBase:0x%.8x\tAllocationProtect:0x%.8x\tReginSize:%u\tState:0x%.8x\tProtect:0x%.8x\tType:0x%.8x\n",mbi.BaseAddress,mbi.AllocationBase 

        ,mbi.AllocationProtect 

        ,mbi.RegionSize,mbi.State 

        ,mbi.Protect,mbi.Type); 

    printf("Free:DEDOMMIT.\n"); 

    if(!VirtualFree(lpRound,sizeVirtual,MEM_DECOMMIT)){ 

        printf("Virtual free error:%d\n",GetLastError()); 

    printf("Free:RELEASE.\n"); 

    if(!VirtualFree(lpAddress,0,MEM_RELEASE)){ 

    system("pause"); 

    return 0; 

<a target="_blank" href="http://blog.51cto.com/attachment/201107/111305751.png"></a>

本文轉hackfreer51CTO部落格,原文連結:http://blog.51cto.com/pnig0s1992/625126,如需轉載請自行聯系原作者

繼續閱讀