天天看点

内存管理基础[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,如需转载请自行联系原作者

继续阅读