天天看點

記憶體管理基礎[2]

/*Code By Pnig0s1992*/ 

#include <Windows.h> 

#include <stdio.h> 

#include <tchar.h> 

#define MEM_BLOCK_SIZE 32 

BOOL ShowMemContent(LPVOID lpMem,SIZE_T dwSize); 

int main(void){ 

    HANDLE hHeap = GetProcessHeap(); 

    LPVOID lpSrc; 

    LPVOID lpDis; 

    if(hHeap == NULL){ 

        printf("擷取目前程序中的堆錯誤:%d\n",GetLastError()); 

        return 1; 

    } 

    lpSrc = HeapAlloc(hHeap,0,MEM_BLOCK_SIZE); 

    lpDis = HeapAlloc(hHeap,0,MEM_BLOCK_SIZE); 

    printf("未清零的記憶體:\n"); 

    ShowMemContent(lpSrc,MEM_BLOCK_SIZE); 

    ShowMemContent(lpDis,MEM_BLOCK_SIZE); 

    ZeroMemory(lpSrc,MEM_BLOCK_SIZE); 

    ZeroMemory(lpDis,MEM_BLOCK_SIZE); 

    printf("清零後的記憶體:\n"); 

    FillMemory(lpSrc,MEM_BLOCK_SIZE,0xBB); 

    FillMemory(lpSrc,MEM_BLOCK_SIZE/2,0xAA); 

    CopyMemory(lpDis,lpSrc,MEM_BLOCK_SIZE); 

    printf("填充複制後的記憶體:\n"); 

    system("pause"); 

    return 0; 

BOOL ShowMemContent(LPVOID lpMem,SIZE_T dwSize){ 

    BYTE lpShow[MEM_BLOCK_SIZE]; 

    INT i=0; 

    if(dwSize>MEM_BLOCK_SIZE){ 

        printf("over flow."); 

        return FALSE; 

    CopyMemory(lpShow,lpMem,MEM_BLOCK_SIZE); 

    for(i=0;i<dwSize;i++){ 

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

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

            printf("\n"); 

        } 

    printf("\n"); 

    return TRUE; 

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

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

繼續閱讀